Lansrow
Lansrow

Reputation: 23

JLabels not showing up

I am trying to figure why I can't see my labels like when I try to put 2 labels into 1 panel they dissapear, the only way I can seem to get it to work is if I add everything to JFrame with no type of hierarchy.

import javax.swing.*;

import java.awt.*;

public class GUI extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    static JRadioButton tirebg1 = new JRadioButton();
    static JRadioButton tirebg2 = new JRadioButton();
    static JRadioButton tirebg3 = new JRadioButton();

    static ButtonGroup tirebg = new ButtonGroup();

    public static void main(String[] args) {
        Car cspeed = new Car();
        int carspeed = cspeed.getSpeed();
        Motorcycle mspeed = new Motorcycle();
        int motospeed = mspeed.getSpeed();
        Truck tspeed = new Truck();
        int truckSpeed = tspeed.getSpeed();
        JRadioButton wide = new JRadioButton();
        JLabel tbuttons = new JLabel();
        JPanel topPane = new JPanel();
        tirebg.add(tirebg1);
        tirebg.add(tirebg2);
        tirebg.add(tirebg3);
        JFrame GUIframe = new JFrame();
        JLabel label1 = new JLabel();
        label1.setLayout(new FlowLayout());
        JLabel tireLabel = new JLabel();
        String[] names = new String[5];
        names[0] = "Car";
        names[1] = "Truck";
        names[2] = "Motorcycle";
        String[] hello = new String[5];
        GUIframe.setSize(500, 500);
        GUIframe.setDefaultCloseOperation(EXIT_ON_CLOSE);
        JList list = new JList(names);
        list.setBorder(BorderFactory.createRaisedSoftBevelBorder());
        label1.add(list);
        tireLabel.add(tirebg1);
        tireLabel.add(tirebg2);
        tireLabel.add(tirebg3);
        topPane.add(tbuttons);
        topPane.add(tireLabel);
        topPane.setLayout(new FlowLayout());

        label1.setBackground(Color.cyan);
        GUIframe.add(topPane);
        GUIframe.validate();
        GUIframe.setBackground(Color.GREEN);
        GUIframe.setVisible(true);
    }
}

Upvotes: 1

Views: 1387

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Your code has several issues, but the reason that you're not seeing the tireLabel or the tbuttons component is because you're using a JLabel. Understand that JLabel is not built to act as a container for other components. The key concept is that it calculates its preferred size based on the text it holds and/or the icon it holds and (and this is key) not on the sizes or preferred sizes of any components it might hold.

The solution is to not use JLabel for a purpose it wasn't intended for but rather to use a JPanel which does adjust its own preferred size depending on the sizes of its held components and its layouts.

Other unrelated issues:

  • Your program extends JFrame but never uses itself as a JFrame, something that will confuse anyone who reads your code. If you're not going to use the instance of the class as a JFrame, then don't extend the class.
  • Your program isn't an OOP-compliant program, one with instance fields, public methods, and such, but rather is little more than one large static main method, and this will result in a large God-method, one with too much responsibility, and one that is very difficult to debug and to maintain. Don't throw out the OOP baby with the bath water -- Create Swing GUI's in a well-behaved OOP-compliant way.
  • You're trying to set background colors to components that are not opaque (a JLabel), to components that are never added to the GUI (label1), or are not fully displayed (the JFrame).
  • You're using FlowLayout an awful lot, and in places where other layouts would probably serve you better. It's as if it's the only layout that you know how to use, and so you use it. Try branching out and using other layouts including GridLayout for your JRadioButton container and perhaps BorderLayout for the main container (JPanel).

Upvotes: 3

Frakcool
Frakcool

Reputation: 11143

Since you posted a lot of code and I'm not sure what were you trying to achieve, I modified your code adding 3 JLabels at the topPane. And 3 JRadioButtons (I didn't add the ButtonGroup) below on a second JPanel, I commented how to make them appear on a vertical and horizontal align.

Something you should take into account is:

  • Don't extend and create objects from JFrame (One or the other, not both, I recommend you to create objects).

  • You were giving your JPanel a Layout after adding components to it, it should be done before.

  • From the above point, you were also giving your Layout to your JLabel not your JPanel.

  • You were adding a JList into a JLabel.

  • You missed to have a class constructor too.

  • Don't have multiple JFrames for more see The use of multiple JFrames, Good / Bad practice

  • Next time post a code which has no dependencies such as your Truck, Car and Motorcycle classes (i.e. a Runnable example). And use plain text instead so we can copy-paste the code and see the issue. Also try posting images (or the link and we can edit to add it).

Now, the outpus of my own program are:

enter image description hereenter image description here

And it was done with the following code.

import javax.swing.*;
import java.awt.*;
public class GUIExample {
    JFrame frame;
    JLabel label1, label2, label3;
    JPanel topPane, radioPane;
    JRadioButton radio1, radio2, radio3;
    public static void main(String[] args) {
        new GUIExample();
    }

    GUIExample () {
        frame = new JFrame();

        topPane = new JPanel();
        radioPane = new JPanel();
        topPane.setLayout(new FlowLayout());
        // radioPane.setLayout(new BoxLayout(radioPane, BoxLayout.PAGE_AXIS)); //Vertical align
        radioPane.setLayout(new FlowLayout()); //Horizontal align

        label1 = new JLabel("Car");
        label2 = new JLabel("Motorcycle");
        label3 = new JLabel("Truck");

        radio1 = new JRadioButton("Radio1");
        radio2 = new JRadioButton("Radio2");
        radio3 = new JRadioButton("Radio3");

        topPane.add(label1);
        topPane.add(label2);
        topPane.add(label3);

        radioPane.add(radio1);
        radioPane.add(radio2);
        radioPane.add(radio3);

        frame.add(topPane, BorderLayout.PAGE_START);
        frame.add(radioPane, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

Upvotes: 4

Related Questions