?
Solved

Help with GUI coding!

Posted on 2003-03-23
6
Medium Priority
?
197 Views
Last Modified: 2010-03-31
Hi,
I've made up code to guess random numbers from 3 text fields. One thing that has been worrying me is the size of the constructor the GUI is within. Is there any way to break up a constructor when using JPanel's and JTabbedPane's(Perhaps making it modular). Can anyone give me a url or any advise?
0
Comment
Question by:edgrey
5 Comments
 

Accepted Solution

by:
Wysiwyg earned 152 total points
ID: 8190373
I always use a jbInit() method to do all the gui work.
Something like

public class foo extends JFrame //JDialog ?
{
  GridBagLayout gbl = new GridBagLayout();
  JPanel infoPanel = new JPanel();
  JPanel buttonPanel = new JPanel();
 
  JLabel nameLabel = new JLabel();
  JButton ok = new JButton();

  public foo()
  {
    jbInit();
  }

  protected void jbInit()
  {
    JPanel contentPanel = (JPanel)this.getContentPane();
    //...
    infoPanel.add(nameLabel);
    //...
    buttonPanel.add(ok);
    //...
    contentPanel.setLayout(gbl);
    contentPanel.add(infoPanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 10, 5, 10), 0, 0));
   
    contentPanel.add(buttonPanel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(5, 10, 5, 10), 0, 0));
  }
  //...
}
0
 
LVL 2

Assisted Solution

by:bkrahmer
bkrahmer earned 148 total points
ID: 8192750
I have great respect for functions which are short, but I think 1 line is arbitrarily too short.  There is no point in that.  I think that all functions should fit on your screen and (ideally) do only one thing.  The best answer I think is to break down the problem.  You could try something like this:

public myConstructor
{
  createLayoutManagers();
  createPanels();
  createDrawingPanel();
  setupWindowOptions();
  ..//blah blah blah
}

I think this will make it much easier to read.

brian
0
 

Expert Comment

by:CleanupPing
ID: 9058532
edgrey:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9730819
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept bkrahmer's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9730830
Correction - Points to be split between Wysiwig and bkrahmer.

Sorry Wysiwig, I initially missed your comment.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question