| | |
Nesting JComponents together?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
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
TimeComponent.java
DayComponent.java
Tester.java
Java Syntax (Toggle Plain Text)
import java.awt.*; import javax.swing.*; public class DayTester { public static void main(String[] args) { JFrame frame = new JFrame(); TimeComponent time = new TimeComponent(); DayComponent day = new DayComponent(); frame.add(time); frame.add(day); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setVisible(true); } private static final int FRAME_WIDTH = 1000; private static final int FRAME_HEIGHT = 1000; }
TimeComponent.java
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class TimeComponent extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; Rectangle rect = new Rectangle(0, 0, 50, 25); g2.drawString("Time", (int)rect.getWidth() / 4, (int)rect.getHeight()); g2.draw(rect); String time = null; for (int i = 0; i < 24; i++) { if (i == 0) time = Integer.toString(12) + " AM"; else if (i > 0 && i < 12) time = Integer.toString(i) + " AM"; else if (i == 12) time = Integer.toString(12) + " PM"; else time = Integer.toString(i - 12) + " PM"; rect.translate(0, (int)rect.getHeight()); g2.drawString(time, (int)(rect.getX() + rect.getWidth() / 4), (int)(rect.getY() + rect.getHeight())); g2.draw(rect); } } }
DayComponent.java
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class DayComponent extends JComponent { public DayComponent() { events = new String[10]; events[0] = "Soccer"; events[9] = "Piano"; } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; Rectangle rect = new Rectangle(50, 0, 200, 25); rect.translate(0, (int)rect.getHeight()); g2.drawString(events[0], (int)(rect.getX() + rect.getWidth() / 4), (int)(rect.getY() + rect.getHeight())); g2.draw(rect); } private String[] events; }
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
----------------------------------------------
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
Ah, didn't really pay attention to that.
You don't add things directly to the JFrame. You add them to it's contentPane
You don't add things directly to the JFrame. You add them to it's contentPane
Java Syntax (Toggle Plain Text)
frame.getContentPane().setLayout(new FlowLayout()); 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
----------------------------------------------
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
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.
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
----------------------------------------------
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
![]() |
Other Threads in the Java Forum
- Previous Thread: how to send login info to yahoo server
- Next Thread: alpe gulay's program to create a diamond using asterisk...
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows






