943,923 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 826
  • Java RSS
May 6th, 2008
0

Nesting JComponents together?

Expand Post »
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

Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
May 6th, 2008
0

Re: Nesting JComponents together?

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
May 6th, 2008
0

Re: Nesting JComponents together?

I tried both. Nothing gets drawn to the panel.

frame.setLayout(new FlowLayout());

Can JComponents be nested?
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
May 6th, 2008
0

Re: Nesting JComponents together?

Ah, didn't really pay attention to that.

You don't add things directly to the JFrame. You add them to it's contentPane
Java Syntax (Toggle Plain Text)
  1. frame.getContentPane().setLayout(new FlowLayout());
  2. frame.getContentPane().add(component);
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
May 6th, 2008
0

Re: Nesting JComponents together?

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??
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
May 6th, 2008
0

Re: Nesting JComponents together?

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 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: how to send login info to yahoo server
Next Thread in Java Forum Timeline: alpe gulay's program to create a diamond using asterisk...





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


Follow us on Twitter


© 2011 DaniWeb® LLC