PDA

View Full Version : Java constructor question


suryad
04-27-2005, 04:53 AM
So I got a Java constructor question...suppose I am creating a gui thru a constructor like so:

public EmailFrame()
{
// SETS UP THE UI HERE
}


Now I am required to create a similar gui but with certain fields different...like the window title of the frame for example and so I create a constructor like so:

public EmailFrame(String windowTitle)
{
// SETS UP THE UI HERE + THE CODE FOR THE TITLE


So now we have a bad programming situation here. I have a default constructor that does not take any arguments but the code is similar to the other constructor that DOES take an argument and everything is the exact same other than the one line that deals with the window title. How do I go about so that I dont have to re-call the code of the first constructor with minimal work? I have some ideas but before I try I would like to receive some feedback. Thanks in advance.

Spookster
04-27-2005, 05:27 AM
Why are you doing everything in the constructor? You should just use the constructor to initialize the data members in that class. Create some methods within your class to set up the UI.

shmoove
04-27-2005, 09:29 AM
This isn't specifically related to constructors, but to any method. Basically, what I usually do is make one "main" method that takes all the possible parameters and all the methods that take less parameters call this one with default values. In your case it would look like:

public EmailFrame() {
this("Default Window Title");
}

public EmailFrame(String windowTitle) {
// set up whatever
}

For a regular method (not a constructor), it would look like:

public void someMethod() {
someMethod(0);
}

public void someMethod(int param) {
// bla bla bla
}


shmoove

suryad
04-27-2005, 08:47 PM
Thanks for the great suggestions fellas. I am gonna digest your suggestions for a while and then decide what to do. Yes I think some methods are gonna be in order that the constructor will simply call. Shmoove your suggestion is very intriguing. Be back when I have something concrete to report. Take care fellas and thanks a lot once again. Much appreciated. :thumbsup:

suryad
04-27-2005, 09:47 PM
Schmoove your suggestion has intrigued me but how would I go about achieving it. The following listings are the 2 constructors I am using as of now to make everything work.

Constructor 1:

public EmailFrame()
{
openFrameCount++;
setTitle("Email Message " + openFrameCount);

JPanel top = new JPanel();
top.setBorder(new EmptyBorder(10, 10, 10, 10));
top.setLayout(new BorderLayout());
top.add(buildAddressPanel(), BorderLayout.NORTH);

content = new JTextArea( 15, 30 );
content.setBorder( new EmptyBorder(0,5 ,0, 5) );
content.setLineWrap(true);

JScrollPane textScroller = new JScrollPane(content, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
top.add( textScroller, BorderLayout.CENTER);

JButton sendBtn = new JButton("Send");
top.add(sendBtn, BorderLayout.SOUTH);

sendBtn.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
sendBtnItemActionPerformed(evt);
}
});

setContentPane(top);
pack();
}


Constructor 2:

public EmailFrame(String emailToField, String subjField, String bodytext)
{
openFrameCount++;
setTitle("Email Message " + openFrameCount);

JPanel top = new JPanel();
top.setBorder(new EmptyBorder(10, 10, 10, 10));
top.setLayout(new BorderLayout());
top.add(buildAddressPanel(), BorderLayout.NORTH);

toField.setText(emailToField); // Extra
toField.setEditable(false); // Extra
subField.setText(subjField); // Extra
subField.setEditable(false); // Extra

content = new JTextArea( 15, 30 );
content.setBorder( new EmptyBorder(0,5 ,0, 5) );
content.setLineWrap(true);
content.setText(bodytext);

JScrollPane textScroller = new JScrollPane(content, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
top.add( textScroller, BorderLayout.CENTER);

JButton sendBtn = new JButton("Send");
top.add(sendBtn, BorderLayout.SOUTH);

sendBtn.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
sendBtnItemActionPerformed(evt);
}
});

setContentPane(top);
pack();
}


The lines marked extra are the lines that are different. So the idea is to have the lowest common denominator in one constructor and the mods in the rest. The only way I can think of by achieving this is by doing:

Public EmailFrame()
{
initGUIComponents(); // dump gui code here
}

And the second constructor would be:

Public EmailFrame()
{
initGUIComponents(); // dump gui code here
// the lines of code that are commented as extra
}


Is that right?

shmoove
04-28-2005, 09:06 AM
It's not exactly what I suggested but it will do just as good (and maybe even better in this case).

shmoove