Results 1 to 10 of 10

Thread: Java constructor question

  1. #1
    Senior Member
    Join Date
    Feb 2005
    Location
    Folsom, CA
    Posts
    221
    Thanks
    0
    Thanked
    0 times in 0 posts

    Java constructor question

    So I got a Java constructor question...suppose I am creating a gui thru a constructor like so:
    Code:
    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:
    Code:
    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.

  2. #2
    cat /dev/null streetster's Avatar
    Join Date
    Jul 2003
    Location
    London
    Posts
    4,138
    Thanks
    119
    Thanked
    100 times in 82 posts
    • streetster's system
      • Motherboard:
      • Asus P7P55D-E
      • CPU:
      • Intel i5 750 2.67 @ 4.0Ghz
      • Memory:
      • 4GB Corsair XMS DDR3
      • Storage:
      • 2x1TB Drives [RAID0]
      • Graphics card(s):
      • 2xSapphire HD 4870 512MB CrossFireX
      • PSU:
      • Corsair HX520W
      • Case:
      • Coolermaster Black Widow
      • Operating System:
      • Windows 7 x64
      • Monitor(s):
      • DELL U2311
      • Internet:
      • Virgin 50Mb
    Could you have something like

    Code:
    public EmailFrame()
    {
    EmailFrame("Default Window Title Goes Here")
    }
    or

    Code:
    public EmailFrame()
    {
    this("Default Window Title Goes Here")
    }
    type thing to avoid code replication? There's prolly much better ways, compared to my javaq noob skills...
    Last edited by streetster; 27-04-2005 at 08:50 AM.

  3. #3
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    Well i would've said to overload the constructor but obviously that's going to cause code replication. You could do it as follows:

    Code:
    public EmailFrame() {
    setTitle("Default");
    createUI(); // this method invokes the UI
    }
    
    public EmailFrame(String title) {
    setTitle(title);
    createUI();
    }
    When you're extending the JFrame class for example, in the constructor, everything will apply to the parent frame so you can simply do setTitle() and then do whatever you want in another method

  4. #4
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    Could you take a boolean value as an argument and then nest the JFrame title in an if..else

    if(!true)
    super("Window title one");
    else
    super("Window title two");
    To err is human. To really foul things up ... you need a computer.

  5. #5
    Senior Member
    Join Date
    Feb 2005
    Location
    Folsom, CA
    Posts
    221
    Thanks
    0
    Thanked
    0 times in 0 posts
    Thanks guys for all the replies. I agree with all of your suggestions. I have a complicated little gui going on here...and if you guys are interested just let me know and I can email you guys my code. I am gonna go with Kezzer's suggestion as of now because I do have a lot of code in the constructors that are being replicated. So I am going to take the common code and put it in a function as was suggested and then tweak from there on. Once again your help is highly appreciated. Take care guys.

  6. #6
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    overloading and putting common code into methods is usually the best idea

  7. #7
    Senior Member
    Join Date
    Feb 2005
    Location
    Folsom, CA
    Posts
    221
    Thanks
    0
    Thanked
    0 times in 0 posts
    The following listings are the 2 constructors I am using as of now to make everything work.

    Constructor 1:
    Code:
        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:
    Code:
     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:
    Code:
    Public EmailFrame()
    {
        initGUIComponents(); // dump gui code here
    }
    And the second constructor would be:
    Code:
    Public EmailFrame()
    {
        initGUIComponents(); // dump gui code here
    // the lines of code that are commented as extra 
    }
    Is that right Kezzer? That what you would recommend?

  8. #8
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    Yup, what you put below looks fine although you didn't include the parameters you were going to include in the constructor in the last example i presume?

  9. #9
    Senior Member
    Join Date
    Feb 2005
    Location
    Folsom, CA
    Posts
    221
    Thanks
    0
    Thanked
    0 times in 0 posts
    Yes thats correct. Thanks a lot.

  10. #10
    Sam
    Sam is offline
    Registered+
    Join Date
    Oct 2003
    Posts
    22
    Thanks
    0
    Thanked
    0 times in 0 posts
    Leave the set up code in the constructor and create a method called resetGui() or something to set up the defaults on it.

    Then call this() on your constructor as Kezzer suggested.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Best book to learn JAVA 2?
    By Taz in forum Software
    Replies: 16
    Last Post: 27-08-2005, 08:54 PM
  2. Java Linux
    By Raz316 in forum Software
    Replies: 8
    Last Post: 06-04-2005, 09:35 AM
  3. Potential HUGE source of free Java games / apps
    By PriestJPN in forum Smartphones and Tablets
    Replies: 4
    Last Post: 03-11-2004, 01:59 AM
  4. Java
    By Jonny M in forum Software
    Replies: 8
    Last Post: 24-12-2003, 02:33 PM
  5. Java vs .NET
    By DaBeeeenster in forum Software
    Replies: 2
    Last Post: 11-09-2003, 02:47 PM

Posting Permissions

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