943,630 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1980
  • Java RSS
Sep 11th, 2006
0

Problem with very basic homework... (It's done, but have one bug I can't figure out.)

Expand Post »
Hello all,

I've got an assignment due on thursday that is very easy, but I have one bug that is driving me nuts.

The assigment is to just create a basic GUI with 1 label, a text box, and a few check boxes, so it's not that hard. The problem is that it compiles fine with no errors or anything, but when I run it, nothing is showing up in the window unless I use the mouse to click the lower right corner of the window and resize it. Then the items show up.

It has been about 2 years since I've taken CS I (Got all the other stuff out of the way :mrgreen: ) but now, I'm a little rusty...

Here is my code:

Java Syntax (Toggle Plain Text)
  1. /*
  2.  * Tiny App
  3.  *
  4.  */
  5.  
  6. import javax.swing.*;
  7. import java.awt.*;
  8.  
  9. public class TinyApp
  10. {
  11. public static void main(String[] args)
  12. {
  13. // Set-up JFrame
  14. JFrame jfWindows = new JFrame("Search Box");
  15. jfWindows.setSize(400, 150);
  16. jfWindows.setVisible(true);
  17.  
  18. // Set up Container for controls
  19. Container cp = jfWindows.getContentPane();
  20. cp.setLayout(new BorderLayout());
  21.  
  22. // Pane to hold the search box and label
  23. JPanel jpSearch = new JPanel();
  24. jpSearch.setLayout(new FlowLayout(FlowLayout.CENTER));
  25. cp.add(jpSearch, BorderLayout.NORTH);
  26.  
  27. // Search label
  28. JLabel lblSearch = new JLabel("Enter key phrase: ");
  29. jpSearch.add(lblSearch);
  30.  
  31. // Search text box
  32. JTextField jtfSearch = new JTextField(20);
  33. jpSearch.add(jtfSearch);
  34.  
  35. // Bottom Panel
  36. JPanel jpYear = new JPanel();
  37. jpYear.setLayout(new FlowLayout(FlowLayout.CENTER));
  38. cp.add(jpYear, BorderLayout.SOUTH);
  39.  
  40. // Create Year Checkboxes
  41. JCheckBox jcb1998 = new JCheckBox("1998");
  42. jpYear.add(jcb1998);
  43. JCheckBox jcb1999 = new JCheckBox("1999");
  44. jpYear.add(jcb1999);
  45. JCheckBox jcb2000 = new JCheckBox("2000");
  46. jpYear.add(jcb2000);
  47. JCheckBox jcb2001 = new JCheckBox("2001");
  48. jpYear.add(jcb2001);
  49. }
  50. }
Thank you in advance for any help. This but is driving me nuts. I have even shown it to a couple of the TA's in the CS lab and they are a bit baffled by it.

Thank you,

Paul Allen (Antiparadigm)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Antiparadigm is offline Offline
3 posts
since Sep 2006
Sep 11th, 2006
0

Re: Problem with very basic homework... (It's done, but have one bug I can't figure out.)

After you have finished adding child components (ie the end of the program) you need to add the following:
Java Syntax (Toggle Plain Text)
  1. jfWindows.pack();
I would also suggest adding:
Java Syntax (Toggle Plain Text)
  1. jfWindows.setDefaultCloseOperation(EXIT_ON_CLOSE);
The default is HIDE_ON_CLOSE, which leaves the process running until you explicitly exit the IDE/Runtime Environment.
Last edited by DavidRyan; Sep 11th, 2006 at 9:24 pm.
Reputation Points: 22
Solved Threads: 11
Posting Whiz in Training
DavidRyan is offline Offline
229 posts
since Jul 2006
Sep 11th, 2006
0

Re: Problem with very basic homework... (It's done, but have one bug I can't figure out.)

You could also try adding all your components and THEN showing the JFrame.

Java Syntax (Toggle Plain Text)
  1. JFrame jfWindows = new JFrame("Search Box");
  2. ...
  3. //add components here...
  4. ...
  5. jfWindows.setVisible(true);
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005
Sep 11th, 2006
0

Re: Problem with very basic homework... (It's done, but have one bug I can't figure o

That was right on DavidRyan.

The line:
Java Syntax (Toggle Plain Text)
  1. jfWindows.setDefaultCloseOperation(EXIT_ON_CLOSE);
Has to be implemented in an action listener right?
Last edited by Antiparadigm; Sep 11th, 2006 at 11:18 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Antiparadigm is offline Offline
3 posts
since Sep 2006
Sep 12th, 2006
0

Re: Problem with very basic homework... (It's done, but have one bug I can't figure o

The line:
Java Syntax (Toggle Plain Text)
  1. jfWindows.setDefaultCloseOperation(EXIT_ON_CLOSE);
Has to be implemented in an action listener right?
You can just put it in main once you've created the JFrame. It's just a call to a method, afterall
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Sep 12th, 2006
0

Re: Problem with very basic homework... (It's done, but have one bug I can't figure out.)

EXIT_ON_CLOSE use to have to be placed in an ActionListener. Not anymore though.
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005
Sep 12th, 2006
0

Re: Problem with very basic homework... (It's done, but have one bug I can't figure o

Thank you both your help really staightend me out.

Just one quick note:

Java Syntax (Toggle Plain Text)
  1. jfWindows.setDefaultCloseOperation(EXIT_ON_CLOSE);
was having problems with EXIT_ON_CLOSE. After a little research, I found I had to use:

Java Syntax (Toggle Plain Text)
  1. jfWindows.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Again, Thank you so much for your help!

Paul Allen (antiparadigm)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Antiparadigm is offline Offline
3 posts
since Sep 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: KoC Recruiting
Next Thread in Java Forum Timeline: New 2 JAVA





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC