3

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

Community
  • 1
  • 1
Surender Thakran
  • 3,958
  • 11
  • 47
  • 81

2 Answers2

4

Try to use

jfr.add(jl, BorderLayout.PAGE_START);
jfr.add(jb1, BorderLayout.LINE_START);
jfr.add(jb2, BorderLayout.LINE_END);

Important note from Java Docs :

Before JDK release 1.4, the preferred names for the various areas were different, 
ranging from points of the compass (for example, BorderLayout.NORTH for the 
top area) to wordier versions of the constants we use in our examples. 
The constants our examples use are preferred because they are standard and 
enable programs to adjust to languages that have different orientations.

For more info on BorderLayout, see How to use BorderLayout

To actually know as to why you got that error I simply wrote these two lines in the code

System.out.println("SwingConstants.NORTH : " + SwingConstants.NORTH);
System.out.println("BorderLayout.PAGE_START : " + BorderLayout.PAGE_START);

which gave an output as below :

SwingConstants.NORTH : 1
BorderLayout.PAGE_START : First

Looking at the output you can understand, what is the issue, in using the SwingContants values over BorderLayout Constraints.

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • You're MOST WELCOME and KEEP SMILING :-). Though as to why your error came do look at this latest edit of mine. – nIcE cOw Jan 06 '13 at 16:25
  • `BorderLayout` constants are of type `String` while that of `SwingConstants` are of type `int`. I went through all the methods of `JFrame` even those inherited from `Component` and `Container`, I couldn't find any method like `add(Component c, int i)`. So how did the one with `BorderLayout` worked? – Surender Thakran Jan 06 '13 at 17:01
  • Sorry for the late reply, that was night time (so sleeping :-)). I guess you are looking at the wrong place `IMHO`, since you had added `BorderLayout` to the whole thingy, so you should be looking inside the `BorderLayout` class, which gets called internally, when `doLayout()` is called which inturns call the `LayoutManager`, now inside `BorderLayout` you can see different variants which all take two arguments of the sort `Component` and an `Object` in some cased `Object` being more specific i.e. `String` – nIcE cOw Jan 07 '13 at 04:47
  • To be continued... : Like, as in [addLayoutComponent(comp, "pos")](http://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html#addLayoutComponent(java.awt.Component,%20java.lang.Object)) – nIcE cOw Jan 07 '13 at 04:47
4
  • use BorderLayout instead of SwingConstants, jfr.add(jl, BorderLayout.NORTH);

  • SwingConstants are implemented for TextLayout, not for JComponents layout

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    `BorderLayout.NORTH` - I am starting to use `BorderLayout.PAGE_START` & `BorderLayout.LINE_START` rather than the 'compass' constants because they work better with left-to-right text orientations. It might not be a problem for most apps, but why not use the way that will work for all? +1 – Andrew Thompson Jan 08 '13 at 09:01