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

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2006
Posts: 3
Reputation: Antiparadigm is an unknown quantity at this point 
Solved Threads: 0
Antiparadigm Antiparadigm is offline Offline
Newbie Poster

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

 
0
  #1
Sep 11th, 2006
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:

  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)
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 229
Reputation: DavidRyan is an unknown quantity at this point 
Solved Threads: 11
DavidRyan's Avatar
DavidRyan DavidRyan is offline Offline
Posting Whiz in Training

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

 
0
  #2
Sep 11th, 2006
After you have finished adding child components (ie the end of the program) you need to add the following:
  1. jfWindows.pack();
I would also suggest adding:
  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.
Please anyone, correct me if I am wrong. It is the best way for me to learn.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

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

 
0
  #3
Sep 11th, 2006
You could also try adding all your components and THEN showing the JFrame.

  1. JFrame jfWindows = new JFrame("Search Box");
  2. ...
  3. //add components here...
  4. ...
  5. jfWindows.setVisible(true);
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 3
Reputation: Antiparadigm is an unknown quantity at this point 
Solved Threads: 0
Antiparadigm Antiparadigm is offline Offline
Newbie Poster

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

 
0
  #4
Sep 11th, 2006
That was right on DavidRyan.

The line:
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

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

 
0
  #5
Sep 12th, 2006
Originally Posted by Antiparadigm View Post
The line:
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

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

 
0
  #6
Sep 12th, 2006
EXIT_ON_CLOSE use to have to be placed in an ActionListener. Not anymore though.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 3
Reputation: Antiparadigm is an unknown quantity at this point 
Solved Threads: 0
Antiparadigm Antiparadigm is offline Offline
Newbie Poster

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

 
0
  #7
Sep 12th, 2006
Thank you both your help really staightend me out.

Just one quick note:

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

  1. jfWindows.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Again, Thank you so much for your help!

Paul Allen (antiparadigm)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC