Results 1 to 2 of 2
  1. #1
    MacNstuff is offline Member
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

    Post Where is it best to declare swing components?

    Hello,
    my first post! :D

    Can someone please explain to me the implications of declaring components inside the class constructor verses outside it?

    For example, declaring the components outside the constructor seems to be common:
    Java Code:
    import javax.swing.*;
    class mytest {
    JLabel alabel;
    JCheckBox acheckbox;
    
    mytest() {
    JFrame myf = new JFrame("Hello World");
    myf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    alabel = new JLabel("Just some text");
    acheckbox = new JCheckBox("Some random option");
    myf.add(alabel); myf.add(acheckbox);
    myf.setVisible(true);
    }
    // ... create frame etc...
    but I've also seen the components created inside the constructor too:
    Java Code:
    import javax.swing.*;
    class mytest {
    
    mytest() {
    JFrame myf = new JFrame("Hello World");
    myf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel alabel = new JLabel("Just some text");
    JCheckBox acheckbox = new JCheckBox("Some random option");
    myf.add(alabel); myf.add(acheckbox);
    myf.setVisible(true);
    Now I'm guessing the reason is something to do with the scope of the component - ie making it accessible from another class (in another file), but thats just my guess. Can someone explain this to me please?

    Thx
    M

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import javax.swing.*;
    
    class MyTest {
        // If another method (outside the constructor) needs to
        // see/access these then they will need to be declared
        // here as member variables in class scope.
        // Otherwise it is considered good practive to hide them
        // as local variables. The less you expose the better.
        JLabel alabel;
        JCheckBox acheckbox;
    
        MyTest() {
            // initialize components
            alabel = new JLabel("Just some text");
            acheckbox = new JCheckBox("Some random option");
            // ... create frame etc...
            JFrame myf = new JFrame("Hello World");
            myf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            System.out.println("JFrame default layout manager = " +
                                myf.getLayout().getClass().getName());
            // Use constraints to add components. No constraint adds
            // the component to the center section by default: so
            // you'll only see the last component added.
            myf.add(alabel, BorderLayout.NORTH);
            myf.add(acheckbox, BorderLayout.SOUTH);
    /*
            // Or, you can add both components to a JPanel
            // (FlowLayout default) and add it to a section
            // in the BorderLayout. Many options like this...
            JPanel panel = new JPanel();
            panel.add(alabel);
            panel.add(acheckbox);
            myf.add(panel, BorderLayout.NORTH);
    */
            myf.setVisible(true);
        }
    }

Similar Threads

  1. HTML on Swing Components
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-27-2007, 09:51 AM
  2. Replies: 1
    Last Post: 08-06-2007, 02:17 AM
  3. Printing Swing components in multiple pages
    By Rajeswari in forum New To Java
    Replies: 1
    Last Post: 07-27-2007, 05:49 PM
  4. Printing Swing components in multiple pages
    By Rajeswari in forum AWT / Swing
    Replies: 0
    Last Post: 07-27-2007, 05:01 PM
  5. Gui Components
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-28-2007, 04:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •