Nesting JComponents together?

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

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Nesting JComponents together?

 
0
  #1
May 6th, 2008
I'm having a little trouble trying to nest two component classes (both extend JComponent) together. When I create the frame in my tester file, instantiate each class, and try to add both of them to the frame, it doesn't come out correctly. For instance, I want my frame to display the TimeComponent right next to the DayComponent in this program:

Tester.java

  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class DayTester
  5. {
  6. public static void main(String[] args)
  7. {
  8. JFrame frame = new JFrame();
  9.  
  10. TimeComponent time = new TimeComponent();
  11. DayComponent day = new DayComponent();
  12.  
  13. frame.add(time);
  14. frame.add(day);
  15.  
  16. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17. frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
  18. frame.setVisible(true);
  19.  
  20.  
  21. }
  22. private static final int FRAME_WIDTH = 1000;
  23. private static final int FRAME_HEIGHT = 1000;
  24. }

TimeComponent.java
  1. import java.awt.*;
  2. import java.awt.geom.*;
  3. import javax.swing.*;
  4.  
  5. public class TimeComponent extends JComponent
  6. {
  7. public void paintComponent(Graphics g)
  8. {
  9. Graphics2D g2 = (Graphics2D)g;
  10. Rectangle rect = new Rectangle(0, 0, 50, 25);
  11.  
  12. g2.drawString("Time", (int)rect.getWidth() / 4, (int)rect.getHeight());
  13. g2.draw(rect);
  14.  
  15. String time = null;
  16. for (int i = 0; i < 24; i++)
  17. {
  18. if (i == 0)
  19. time = Integer.toString(12) + " AM";
  20. else if (i > 0 && i < 12)
  21. time = Integer.toString(i) + " AM";
  22. else if (i == 12)
  23. time = Integer.toString(12) + " PM";
  24. else
  25. time = Integer.toString(i - 12) + " PM";
  26.  
  27. rect.translate(0, (int)rect.getHeight());
  28. g2.drawString(time, (int)(rect.getX() + rect.getWidth() / 4), (int)(rect.getY() + rect.getHeight()));
  29. g2.draw(rect);
  30. }
  31. }
  32. }

DayComponent.java
  1. import java.awt.*;
  2. import java.awt.geom.*;
  3. import javax.swing.*;
  4.  
  5. public class DayComponent extends JComponent
  6. {
  7. public DayComponent()
  8. {
  9. events = new String[10];
  10.  
  11. events[0] = "Soccer";
  12. events[9] = "Piano";
  13. }
  14. public void paintComponent(Graphics g)
  15. {
  16. Graphics2D g2 = (Graphics2D)g;
  17. Rectangle rect = new Rectangle(50, 0, 200, 25);
  18.  
  19. rect.translate(0, (int)rect.getHeight());
  20. g2.drawString(events[0], (int)(rect.getX() + rect.getWidth() / 4), (int)(rect.getY() + rect.getHeight()));
  21. g2.draw(rect);
  22. }
  23. private String[] events;
  24. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,391
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 254
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Nesting JComponents together?

 
0
  #2
May 6th, 2008
Frame uses a BorderLayout by default. So, add one West, and one Center, or one Center and one East, or one East and one West, or change the layout to FlowLayout.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Nesting JComponents together?

 
0
  #3
May 6th, 2008
I tried both. Nothing gets drawn to the panel.

frame.setLayout(new FlowLayout());

Can JComponents be nested?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,391
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 254
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Nesting JComponents together?

 
0
  #4
May 6th, 2008
Ah, didn't really pay attention to that.

You don't add things directly to the JFrame. You add them to it's contentPane
  1. frame.getContentPane().setLayout(new FlowLayout());
  2. frame.getContentPane().add(component);
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Nesting JComponents together?

 
0
  #5
May 6th, 2008
hmmm I tried that as well and I still don't get anything in the frame.

I can view each component separately if I only have one added to the frame, but not both. Does it have something to do with extending JComponent??
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,391
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 254
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Nesting JComponents together?

 
0
  #6
May 6th, 2008
Do you do a frame.pack() anywhere? I don't see one. Directly before frame.setSize do frame.pack().

And yes, JComponents can be nested, especially since nearly every Swing element is a JComponent.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC