I am new to Swing and was making a very basic event handling program in Eclipse. Here is the code that i wrote:
public class SwingDemo2 {
JLabel jl;
public SwingDemo2() {
JFrame jfr = new JFrame("Swing Event Handling");
jfr.setSize(250, 100);
jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jl = new JLabel();
jl.setVisible(false);
JButton jb1 = new JButton("OK");
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jl.setText("You Pressed OK");
jl.setVisible(true);
}
});
JButton jb2 = new JButton("Reset");
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jl.setText("You Pressed Reset");
jl.setVisible(true);
}
});
jfr.setLayout(new BorderLayout());
jfr.add(jl, SwingConstants.NORTH);
jfr.add(jb1, SwingConstants.EAST);
jfr.add(jb2, SwingConstants.WEST);
jfr.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SwingDemo2();
}
});
}
}
Eclipse prompts me to open the debug perspective where he shows me the error:
Thread [AWT-EventQueue-0] (Suspended (exception IllegalArgumentException))
EventDispatchThread.run() line: not available [local variables unavailable]
I didn't get any error when I used FlowLayout
instead of BorderLayout
.
I have been trying to find info on the error on the portal, i came across this similar question. The answer is to change a bunch of settings (which didn't help either) without explaining the problem. Please explain the error so that i can make sure not to repeat it. Thanx in advance!
Note: Updated the error message