Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-06-2008, 01:35 AM
Member
 
Join Date: Feb 2008
Posts: 1
MacNstuff is on a distinguished road
Where is it best to declare swing components?
Hello,
my first post!

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:
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:
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-06-2008, 01:59 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML on Swing Components Java Tip Java Tips 0 11-27-2007 10:51 AM
How would I declare the variable numbers as global? barney New To Java 1 08-06-2007 03:17 AM
Printing Swing components in multiple pages Rajeswari New To Java 1 07-27-2007 06:49 PM
Printing Swing components in multiple pages Rajeswari AWT / Swing 0 07-27-2007 06:01 PM
Gui Components Marty New To Java 1 05-28-2007 05:04 AM


All times are GMT +3. The time now is 02:25 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org