JFrame buttons act weird when declaring it to a static variable - java

So, I am having some really weird stuff going on here.
So, my entire class is this:
public class Test extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Test();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void addComponentsToPane(Container pane) {
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 50;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_END;
c.insets = new Insets(10,0,0,0);
c.gridx = 0;
c.gridwidth = 3;
c.gridy = 3;
pane.add(button, c);
}
private JFrame createAndShowGUI(JFrame frame) {
frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
return frame;
}
JFrame frame = null;
public Test() {
createAndShowGUI(frame);
addComponentsToPane(frame.getContentPane());
}
}
So anyway, I am going to focus on this bit:
JFrame frame = null;
public Test() {
createAndShowGUI(frame);
addComponentsToPane(frame.getContentPane());
}
Which, produces this result (which works perfectly fine).
However, when I use this code:
JFrame frame = null;
public Test() {
frame = createAndShowGUI(frame);
addComponentsToPane(frame.getContentPane());
}
Which results in buttons 4 and 5 somehow duplicating themselves with a set size at the top of the screen. Also, buttons 4 and 5 appear to have different sizes.
Like this:
(And when I make the window smaller)
What is causing this? All I am doing is setting a variable from null or a JFrame, which should do nothing.

The first method throws a NullPointerException at the addComponentsToPane method because the frame attribute is never initialized and the second method adds the buttons twice to the frame. I have rewritten the code below and it works fine. You should also have a look at variable shadowing , because your frame attribute gets shadowed by the frame local variable in the addComponentsToPane method whick is rarely a good idea.
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Test();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void addComponentsToPane(Container pane) {
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
button = new JButton("Button 1");
if (true) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 50;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_END;
c.insets = new Insets(10, 0, 0, 0);
c.gridx = 0;
c.gridwidth = 3;
c.gridy = 3;
pane.add(button, c);
}
private void createAndShowGUI() {
frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
JFrame frame = null;
public Test() {
createAndShowGUI();
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}

Related

JFrame Buttons and GridBagConstraints

Why are my constraints for my buttons not working? I looked at the Java Docs and am doing the same thing the tutorials are doing, but for me the buttons stay the same regardless of what gridx, y, width, or fill I use. Any ideas? Here's my code:
class MyWindow
{
public static void main(String [] arg)
{
MyJFrame f = new MyJFrame("My GUI 2015");
f.setVisible(true);
f.setSize(10, 20);
f.add(f.p);
}
}
and
public class MyJFrame extends JFrame {
public JPanel p;
JButton close = new JButton("close");
JButton drawing = new JButton("drawing");
JButton image = new JButton("image");
JButton browser = new JButton("browser");
public MyJFrame(String title) {
super(title);
p = new JPanel();
buildButtons();
}
void buildButtons() {
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(0,40,0,150);
c.gridx = 0;
c.gridy = 0;
p.add(drawing, c);
c.gridx = 2;
c.gridy = 0;
p.add(close, c);
c.insets = new Insets(50,225,50,150);
c.gridx = 0;
c.gridy = 1;
p.add(image, c);
c.insets = new Insets(0,125,0,125);
c.gridx = 0;
c.gridy = 100;
c.gridwidth = 3;
c.fill = GridBagConstraints.HORIZONTAL;
p.add(browser, c);
}
}
The LayoutManager for the container is not specified in your current code (the default for a JPanel is FlowLayout). If you wish to use a GridBagLayout on the container, you must explicitly specify the LayoutManager:
p = new JPanel(new GridBagLayout());
//or
p.setLayout(new GridBagLayout());

ActionListener in JFrames

There is an issue with the code where the ActionListener will not pick up on any action when i call upon it with the buttons. I have done this before but this is my first time attempting a GridBagLayout and it doesn't seem to be working correctly. The GUI is fully functional. What could I do to get the ActionListener to work? Here is the entire class.
package body;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class mainFrame extends JFrame implements ActionListener{
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
static String string = "default";
static JButton one, two, three, four, five, six, seven, eight, nine, zero;
static JButton plus, minus, multiply, divide, equals;
public void init() {
one.setActionCommand("one");
one.addActionListener(this);
two.setActionCommand("two");
two.addActionListener(this);
three.setActionCommand("three");
three.addActionListener(this);
four.setActionCommand("four");
four.addActionListener(this);
five.setActionCommand("five");
five.addActionListener(this);
six.setActionCommand("six");
six.addActionListener(this);
seven.setActionCommand("seven");
seven.addActionListener(this);
eight.setActionCommand("eight");
eight.addActionListener(this);
nine.setActionCommand("nine");
nine.addActionListener(this);
zero.setActionCommand("zero");
zero.addActionListener(this);
plus.setActionCommand("plus");
plus.addActionListener(this);
minus.setActionCommand("minus");
minus.addActionListener(this);
multiply.setActionCommand("multiply");
multiply.addActionListener(this);
divide.setActionCommand("divide");
divide.addActionListener(this);
equals.setActionCommand("equals");
equals.addActionListener(this);
}
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
c.fill = GridBagConstraints.HORIZONTAL;
}
if (shouldWeightX) {
c.weightx = 0.5;
}
JLabel display = new JLabel(string);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
pane.add(display, c);
one = new JButton("1");
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
pane.add(one, c);
two = new JButton("2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
pane.add(two, c);
three = new JButton("3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 1;
pane.add(three, c);
plus = new JButton("+");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 3;
c.gridy = 1;
pane.add(plus, c);
four = new JButton("4");
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
pane.add(four, c);
five = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 2;
pane.add(five, c);
six = new JButton("6");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 2;
pane.add(six, c);
minus = new JButton("-");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 3;
c.gridy = 2;
pane.add(minus, c);
seven = new JButton("7");
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
pane.add(seven, c);
eight = new JButton("8");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 3;
pane.add(eight, c);
nine = new JButton("9");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 3;
pane.add(nine, c);
multiply = new JButton("*");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 3;
c.gridy = 3;
pane.add(multiply, c);
zero = new JButton("0");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 4;
pane.add(zero, c);
equals = new JButton("Equals");
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 1.0;
c.gridx = 1;
c.gridwidth = 2;
c.gridy = 4;
pane.add(equals, c);
divide = new JButton("/");
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_END;
c.weightx = 0.5;
c.gridx = 3;
c.gridy = 4;
pane.add(divide, c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "one") {
System.out.println("hello");
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Besides the == and equals I pointed out, you never call init(), which initializes your action commands.
Also note, you class is already a JFrame. Either use the class JFrame or use the instance JFrame.
You should fix the above problem so that you can properly call your init() method.
Try this
private static void createAndShowGUI() {
mainFrame frame = new mainFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
addComponentsToPane(frame.getContentPane());
frame.init(); <<<<<================== Don't forget about meeeee!
frame.pack();
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if ("one".equals(e.getActionCommand())) {
System.out.println("hello");
}
}
Also you should be following Java naming convention. Class names begin with capital letters. So mainFrame → MainFrame

Vertical filling of GridBagLayout in Java does not work

I am having troubles getting the layout right.
I have a Class which extends JFrame and has a JPanel which has a the GridBagLayout as layout manager. I want 4 buttons which should be layout in this way
Somehow the vertical filling does not work.
I tried various ways but can't figure out how to make this work.
Sorry if this is a noob question, but tried it for more than an hour without any change :/
public class ButtonPanel extends JFrame {
private static final long serialVersionUID = 1L;
public ButtonPanel() {
this.setSize(800, 600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
frame();
}
JButton objectsBtn = new JButton("Objekte"); //TODO Strings einfügen
JButton tenantBtn = new JButton("Mieter"); //TODO Strings einfügen
JButton ownerBtn = new JButton("Eigentümer"); //TODO Strings einfügen
JButton optionsBtn = new JButton("Einstellungen"); //TODO Strings einfügen
JPanel panel = new JPanel();
public void frame(){
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.fill = GridBagConstraints.BOTH;
c.gridwidth = GridBagConstraints.RELATIVE;
c.gridheight = GridBagConstraints.RELATIVE;
//Elemente
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
panel.add(objectsBtn, c);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 1;
c.gridy = 0;
panel.add(optionsBtn, c);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 1;
panel.add(ownerBtn, c);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 1;
c.gridy = 1;
panel.add(tenantBtn, c);
this.add(panel);
objectsBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
new Object();
}
});
ownerBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//TODO Eigentümer erstellen
}
});
}
}
greets
THE-E
EDIT: Found the bug
I used Seaglass as Look and Feel in the main-method
//Set Theme
try {
UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
It works fine with the default Look and Feel Theme.
I have added the answer in the first post. It was a bug caused by the LookAndFeel skin. :/
greets
THE-E

Java Swing - realising a layout with LayeredPane

I have a question regarding making a specific Layout, first I'll show examples then I will add some extra clarification.
Layout when Friends and Messages are closed:
Layout when Friends and Messages are opened:
I intend to make this layout with Java Swing.
My intention is to firstly have the Frame divided in three areas, the top menu row, the main panel and the bottom menu row.
I was thinking of using a BorderLayout for this part.
Then the Friends and Messages buttons should be toggle button's, and on toggle they should show an overlay on top of the mainpanel (or whatever is there), containing a friend list and a message area. I realised I need to use a LayeredPane somehow for this.
Another important part is that the Layout should be viewable in any size, that is the user may resize the application and it will be used on a various amount of resolutions, so I don't really want anything with a fixed width and height.
But I am really lost as how to combine this, so therefore I ask your help.
Hopefully I have explained enough about the situation.
Regards.
this could be about overlay, because JPanel can contains other JComponents
use JLayer (Java7) based on JXLayer(Java6),
use GlassPane with JComponents layed to rellative to....
easiest could be to use JDialog(undecorated) layed to Point (setLocation(int, int)), setVisible() must be wrapped into invokeLater
I will use gridBagLayout.
Here is small example including button which hide your yellow panels:
package Core;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GridBagLayoutDemo {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
add1row(pane);
addmainRow(pane);
addLastRow(pane);
}
private static void addLastRow(Container pane) {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
c.anchor = GridBagConstraints.PAGE_END;
JPanel bottonPanel = new JPanel();
bottonPanel.setBackground(Color.BLUE);
bottonPanel.setLayout(new GridBagLayout());
pane.add(bottonPanel, c);
JPanel messagePanel = new JPanel();
messagePanel.setBackground(Color.GRAY);
messagePanel.add(new JLabel("MESSAGES"));
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_END;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
bottonPanel.add(messagePanel, c);
}
private static void addmainRow(Container pane) {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.CENTER;
JPanel mainManel = new JPanel();
mainManel.setBackground(Color.GREEN);
mainManel.setLayout(new GridBagLayout());
pane.add(mainManel, c);
final JPanel friendListPanel = new JPanel();
friendListPanel.setBackground(Color.YELLOW);
friendListPanel.add(new JLabel("FRIEND LIST"));
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.FIRST_LINE_END;
mainManel.add(friendListPanel, c);
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 1;
c.weightx = 0;
c.weighty = 0;
c.anchor = GridBagConstraints.CENTER;
JButton button = new JButton("On/Off");
mainManel.add(button, c);
final JPanel messageAreaPanel = new JPanel();
messageAreaPanel.setBackground(Color.YELLOW);
messageAreaPanel.add(new JLabel("MESSAGE PANEL"));
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 2;
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.LAST_LINE_END;
mainManel.add(messageAreaPanel, c);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
friendListPanel.setVisible(!friendListPanel.isVisible());
messageAreaPanel.setVisible(!messageAreaPanel.isVisible());
}
});
}
private static void add1row(Container pane) {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.PAGE_START;
Panel headerPanel = new Panel();
headerPanel.setLayout(new GridBagLayout());
headerPanel.setBackground(Color.BLUE);
pane.add(headerPanel, c);
JPanel quitPanel = new JPanel();
quitPanel.setBackground(Color.GRAY);
quitPanel.add(new JLabel("QUIT"));
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_START;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
headerPanel.add(quitPanel, c);
JPanel friendsPanel = new JPanel();
friendsPanel.setBackground(Color.GRAY);
friendsPanel.add(new JLabel("FRIENDS"));
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_END;
c.gridx = 1;
c.gridy = 0;
headerPanel.add(friendsPanel, c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame);
// uncoment to use full screen
// frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
frame.setSize(new Dimension(400, 400));
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

setPopupVisible is canceled if it follows on setvisible true

When I press the button in the program below I want the popup menu from the JComboBox to appear and remain up.
However it only does that when on the second push when the Combobox is already visible. Is there any way I can make it appear on the first push?
public class Problem extends JPanel
implements ActionListener{
private static String SEARCH = "start search";
private static String SELECTED = "Selected";
private JTextField field2;
private JTextField field1;
private JComboBox list = new JComboBox();
public Problem() {
super(new GridBagLayout());
//Construct the panel
field2=new JTextField("");
field1=new JTextField("7564");
JButton search=new JButton("Search");
search.addActionListener(this);
search.setActionCommand(SEARCH);
list.addActionListener(this);
list.setActionCommand(SELECTED);
//Add everything to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 8;
c.gridheight = 3;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.anchor = GridBagConstraints.NORTH;
add(field2,c);
list.setVisible(false);
add(list,c);
c.gridx = 8;
c.gridy = 0;
c.gridwidth = 8;
c.gridheight = 3;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.anchor = GridBagConstraints.NORTH;
add(field1,c);
c.gridx = 16;
c.gridy = 0;
c.gridwidth = 4;
c.gridheight = 3;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.anchor = GridBagConstraints.NORTH;
add(search,c);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand()==SEARCH){
String f2=field2.getText();
String f1=field1.getText();
if(f2.equals("")){
f2=selection(f1);
f2=null;
}
if(f2!=null){
field2.setText(f2);
}
else{
System.out.println("setPopupVisible runs");
field2.setVisible(false);
list.setVisible(true);
field2.setText("");
list.setPopupVisible(true);
}
}
else if(ae.getActionCommand().equals(SELECTED)){
System.out.println("select");
String listtext=(String) list.getSelectedItem();
list.removeAllItems();
if(listtext==null||!listtext.contains(": ")){
return;
}
String f2=listtext.split(": ")[0];
list.setVisible(false);
field2.setVisible(true);
field2.setText(f2);
}
}
private String selection(String sn) {
System.out.println("selection runs");
String name="7";
list.removeActionListener(this);
list.removeAllItems();
list.addItem(name);
list.addActionListener(this);
return null;
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Stuff");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
Problem newContentPane = new Problem();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
frame.setSize(800, 600);
}
public static void main(String[] args) throws FileNotFoundException {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
When toggling visibility of components, make sure the component has completed its internal update before doing custom stuff, that is f.i. delay the call to showing a combo's popup relative to showing the combo itself:
list.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
list.setPopupVisible(true);
}
});

Categories