Results 1 to 5 of 5
- 04-05-2011, 05:10 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Missing something: jlabels not showing up
Hello, I'm trying out my first project with swing in Java and I'm missing something. The code is below. Everything works except when the window is generated it's empty, the labels are being generated (checked with system out messages) but they aren't showing up in the window. I'm not sure if the problem is bewteen the labels in the panel or the panel in the frame but something done broke :)
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class RandomPanel extends JPanel {
public RandomPanel(String label){
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,3));
panel.add(new JLabel(label));
Random r = new Random(); //allows us to generate (pseudo)random values
Integer randInt = 1 + r.nextInt(4095); //random select an int 1-4096
panel.add(new JLabel(randInt.toString()));
panel.add(new JLabel(Integer.toHexString(randInt)));
panel.setVisible(true);
}
}
class Main extends JFrame{
public Main(){
JFrame frame = new JFrame();
frame.setTitle("Hello, Swing");
frame.setLayout(new GridLayout(20,1));
for(int i = 1; i < 21; ++i){
Integer row = i;
RandomPanel panel = new RandomPanel(row.toString());
frame.getContentPane().add(panel);
}
frame.pack();
frame.setVisible(true);
System.out.println("GUI initialized");
}
public static void main (String[] args){
new Main();
}
}
Any suggestions? I'm using Eclipse IDE and Java 6 if it matters.
- 04-05-2011, 05:18 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
In your RandomPanel constructor, you created a new Panel and dressed that with your buttons but you never used that panel anywhere. Don't create a new Panel in that constructor, just set all those things on the RandomPanel with
Java Code:setLayout(new GridLayout(1,3));
- 04-05-2011, 05:24 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Alright I'm very very confused now. I had it the way you say originally and it was doing the same thing, then I added in the panel and it was doing the same thing, but I just took it back out and now it works.
I have no idea why. But that's not your problem. Thank you for the help!
- 04-05-2011, 07:21 AM #4
Extending JPanel and JFrame, as in your example code, is a gross misuse of inheritance. Also, as already said, constructing a JPanel within a class that extends JPanel, and ignoring the JPanel that the class represents, is meaningless.
I'd say you need to go through a tutorial or two.
Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
db
- 04-05-2011, 09:09 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Multiline JLabels
By davetheant in forum New To JavaReplies: 3Last Post: 01-21-2011, 01:18 PM -
can i have a JList of Jlabels?
By chayan in forum AWT / SwingReplies: 3Last Post: 07-04-2010, 12:19 AM -
new JLabels not showing in JPanel (doing revalidate&repaint)
By r00tb33r in forum AWT / SwingReplies: 6Last Post: 06-16-2010, 06:03 AM -
Overlapping JLabels
By techbossmb in forum AWT / SwingReplies: 3Last Post: 09-21-2009, 03:21 PM -
problem with JLabels
By geork in forum New To JavaReplies: 3Last Post: 01-31-2008, 02:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks