Results 1 to 5 of 5
  1. #1
    011121 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default 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.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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));

  3. #3
    011121 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    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!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    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

  5. #5
    011121 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    The assignment specifically says to extend JFrame and Jpanel like that so you;d have to take that up with the professor.

Similar Threads

  1. Multiline JLabels
    By davetheant in forum New To Java
    Replies: 3
    Last Post: 01-21-2011, 02:18 PM
  2. can i have a JList of Jlabels?
    By chayan in forum AWT / Swing
    Replies: 3
    Last Post: 07-04-2010, 01:19 AM
  3. Replies: 6
    Last Post: 06-16-2010, 07:03 AM
  4. Overlapping JLabels
    By techbossmb in forum AWT / Swing
    Replies: 3
    Last Post: 09-21-2009, 04:21 PM
  5. problem with JLabels
    By geork in forum New To Java
    Replies: 3
    Last Post: 01-31-2008, 03:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •