JComboBox not displayed - java

I'm trying to put a JComboBox on my JFrame, if i clicked shows the contents of the combo, but it was not showed on the JFrame.
I already tried with: .setVisible(true), .setEnabled(true), etc.
Here is my code:
public class tryCode {
private final JComboBox vehicleTypeBox = new JComboBox(new String[] {"HELLO WORLD", "OLA K ASE"});
private JFrame frame;
public tryCode() {
frame = new JFrame("");
frame.setSize(new Dimension(300, 300));
frame.setLayout(null);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
showComboBox();
}
public void showComboBox() {
vehicleTypeBox.setBounds(10,80,100,30);
vehicleTypeBox.setSelectedIndex(0);
frame.add(vehicleTypeBox);
}
}
Any solution is welcome!. Thanks

You never call showComboBox() anywhere. If it is supposed to show up at startup you should call it in the constructor.
public tryCode()
{
frame = new JFrame("");
frame.setSize(new Dimension(300, 300));
frame.setLayout(new FlowLayout()); // do not use null!
frame.setResizable(false);
frame.setLocationRelativeTo(null);
showComboBox();
frame.setVisible(true);
}

I just test your code with adding a main method.and it works.no problems.
import java.awt.Dimension;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class TryCode {
private final JComboBox vehicleTypeBox = new JComboBox(new String[] {"HELLO WORLD", "OLA K ASE"});
private JFrame frame;
public TryCode() {
frame = new JFrame("");
frame.setSize(new Dimension(300, 300));
frame.setLayout(null);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
showComboBox();
}
public void showComboBox() {
vehicleTypeBox.setBounds(10,80,100,30);
vehicleTypeBox.setSelectedIndex(0);
frame.add(vehicleTypeBox);
}
/**
* #param args
*/
public static void main(String[] args) {
TryCode t=new TryCode();
}
}

Related

Adding a JPanel from a different class

I'm trying to add a simple JPanel from a different class with only a JButton and a JTextArea into the GUI.
I'm using IntelliJ IDEA for the swing application and there are no errors. However, the JPanel simply doesn't appear/isn't shown.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
public class GUI {
private JPanel mainPanel;
private JPanel mainInner;
public GUI() {
mainPanel.addContainerListener(new ContainerAdapter() {
#Override
public void componentAdded(ContainerEvent e) {
super.componentAdded(e);
System.out.println("component Added");
}
});
}
public void start() {
JFrame frame = new JFrame("GUI");
frame.setContentPane(new GUI().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
InnerPanel inner = new InnerPanel();
mainInner = inner.getMainInner();
inner.setLayout(new BorderLayout());
mainPanel.setLayout(new BorderLayout());
mainPanel.add(mainInner, BorderLayout.CENTER);
mainPanel.revalidate();
mainPanel.repaint();
frame.pack();
frame.setVisible(true);
}
}
The component listener shows that the component has been added, yet it isn't shown.
You forgot to add the mainPanel to frame:
frame.add(mainPanel);

How to constantly update a JPanel

I am trying to make a program where I have two JPanels inside a JFrame, one of which contains a canvas. I am trying to find a way to get that canvas to be constantly updating so that I could create something like a game inside the canvas. I was wondering how I could make it so that the JPanel with the canvas in it is constantly refreshing so that you can see when something is changed in the canvas. Here is my code:
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main
{
private JFrame frame;
private JPanel mainPanel;
private JPanel gamePanel;
private JPanel sidePanel;
public void setup()
{
frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocation(100, 50);
mainPanel = new JPanel();
Dimension d = new Dimension(800, 600);
mainPanel.setMaximumSize(d);
mainPanel.setMinimumSize(d);
mainPanel.setPreferredSize(d);
frame.add(mainPanel);
frame.pack();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
}
public void setupPanels()
{
gamePanel = new JPanel();
Dimension d = new Dimension(600, 600);
gamePanel.setMaximumSize(d);
gamePanel.setMinimumSize(d);
gamePanel.setPreferredSize(d);
gamePanel.setBackground(Color.RED);
mainPanel.add(gamePanel);
sidePanel = new JPanel();
d = new Dimension(600, 600);
sidePanel.setMaximumSize(d);
sidePanel.setMinimumSize(d);
sidePanel.setPreferredSize(d);
sidePanel.setBackground(Color.BLUE);
mainPanel.add(sidePanel);
}
public void setupGame()
{
GameArea game = new GameArea();
gamePanel.add(game);
game.start();
}
public static void main(String[] args)
{
Main main = new Main();
main.setup();
main.setupPanels();
main.setupGame();
}

Displaying a Text Adventure in a GUI

I'm trying to put my text adventure in a GUI,and make it so there are no buttons just text and user input. This is the code for my GUI
import java.awt.*;
import javax.swing.*;
public class Window {
public static void createwindow() {
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Component textLabel = null;
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createwindow(); }
}
I have my text adventure in a separate class and was wondering if there was any easy way to display that in this GUI. Thanks for your help.
import javax.swing.*;
public class Window {
public static void createwindow() {
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Component textLabel = null;
JTextArea jta = new JTextArea();
frame.add(jta);
// frame.pack();
}
public static void main(String[] args) {
createwindow(); }
}
In my understanding, you just want a TextArea.Is right?

JFrame not working - content does not appear

Not sure what my issue is. I created a JFrame and I have a panel that will have 4 large buttons (with graphics - though that isn't coded yet) to show on the frame but I am getting an error when it tries to run this and the panel isn't showing up in the frame.
UPDATED: No error message, but no panel or buttons in the frame...
public class EasyExpress {
private static JFrame frame = new JFrame("EASY BUTTONS");
private JButton WriteBTN = new JButton("Write Email");
private JButton EmailBTN = new JButton("View Emails");
private JButton SolBTN = new JButton("Play Solsuite Solitaire");
private JButton ShutBTN = new JButton("Shutdown Computer");
private JPanel btnPanel;
public EasyExpress() {
/* try {
Image img = ImageIO.read(getClass().getResource("write.jpg"));
WriteBTN.setIcon(new ImageIcon(img));
} catch (IOException ex) {
}*/
btnPanel = new JPanel(new GridLayout(1,4,1,1));
btnPanel.setBounds(0, 0, 1200, 400);
WriteBTN.setPreferredSize(new Dimension(300,400));
EmailBTN.setPreferredSize(new Dimension(300,400));
SolBTN.setPreferredSize(new Dimension(300,400));
ShutBTN.setPreferredSize(new Dimension(300,400));
btnPanel.add(EmailBTN);
btnPanel.add(WriteBTN);
btnPanel.add(SolBTN);
btnPanel.add(ShutBTN);
frame.add(btnPanel);
frame.add(frame);
}
public static void main(String[] args) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setSize(1200,400);
frame.setVisible(true);
}
Essentially, you're adding a frame to another frame, which you simply can't do
You're also not initialising your buttons, which is causing a NullPointerException.
Start by removing extends JFrame, this is just confusing things and as general rule, you should avoid extending from top level containers. Instead, start with a JPanel, for example...
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class EasyExpress extends JPanel {
JButton WriteBTN, EmailBTN, SolBTN, ShutBTN;
JPanel btnPanel;
public EasyExpress() {
btnPanel = new JPanel(new GridLayout(1, 4, 1, 1));
btnPanel.setBounds(0, 0, 1200, 400);
WriteBTN = new JButton("1");
EmailBTN = new JButton("2");
SolBTN = new JButton("3");
ShutBTN = new JButton("4");
WriteBTN.setPreferredSize(new Dimension(300, 400));
EmailBTN.setPreferredSize(new Dimension(300, 400));
SolBTN.setPreferredSize(new Dimension(300, 400));
ShutBTN.setPreferredSize(new Dimension(300, 400));
btnPanel.add(EmailBTN);
btnPanel.add(WriteBTN);
btnPanel.add(SolBTN);
btnPanel.add(ShutBTN);
setLayout(new BorderLayout());
add(btnPanel);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new EasyExpress());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Like #MadProgrammer mentioned in the comments above, you can't have a JFrame inside another JFrame.
However, instead of removing the extends JFrame, I would suggest removing the JFrame inside your EasyExpress object. You already set all the properties for that JFrame in your main, so it will be easier to fix.
Remove JFrame frame = new JFrame("EASY BUTTONS");
Add "EASY BUTTONS" to the EasyExpress object you create in main EasyExpress main = new EasyExpress("EASY BUTTONS");
Remove frame. from in front of frame.add(btnPanel);

JFrame/Panel not showing up

I should be displaying a row of 2 buttons but that doesn't seem to be the case.
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Studying extends JFrame{
JButton button = new JButton("Word");
JButton button1 = new JButton("MoreWords");
public void Studying(){
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1,2));
p1.add(button);
p1.add(button1);
add(p1);
}
public static void main(String[] args){
Studying frame = new Studying();
frame.setTitle("test");
frame.setSize(500,200);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
The constructor is not an actual constructor, it is being treated as a method, causing the classes default constructor to be used. Constructors do not specify a return type, even void.
Fixed Constructor
public Studying(){
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1,2));
p1.add(button);
p1.add(button1);
add(p1);
}
The function Studying() isn't a class, so Studying frame = new Studying(); refers to public class Studying extends JFrame and public void Studying() is never called. Move the creation of the buttons to the static main, and append them to the frame, and the buttons will be visible.
public class Studying extends JFrame {
static JButton button = new JButton("Word");
static JButton button1 = new JButton("MoreWords");
public static void main(String[] args) {
Studying frame = new Studying();
frame.setTitle("test");
frame.setSize(500, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(1, 2));
frame.add(button);
frame.add(button1);
frame.setVisible(true);
}
}
This should work:
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Studying extends JFrame{
JButton button = new JButton("Word");
JButton button1 = new JButton("MoreWords");
public Studying(){
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1,2));
p1.add(button);
p1.add(button1);
add(p1);
}
public static void main(String[] args){
Studying frame = new Studying();
frame.setTitle("test");
frame.setSize(500,200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

Categories