1

So I'm trying to create a gui, I've tinkered with gui's before in java but I'm still new to them. So my issued here is that my JLabels (butLabel & cbLabel) are filled with buttons and checkboxes. Sadly my JFrame will only show whichever is set to the BorderLayout.CENTER. NORTH & SOUTH don't ever show, even if I only set the butLabel to SOUTH and don't even use the cbLabel. What am I overlooking?? It's much appreciated, thanks!

public class mainWindow
{
    JFrame frame = new JFrame("Main Window");

    JLabel butLabel = new JLabel();
    JLabel cbLabel = new JLabel();

    JButton showBut = new JButton("Show");
    JButton exitBut = new JButton("Exit");
    JButton addBut = new JButton("Add");
    JButton remBut = new JButton("Remove");

    JCheckBox aCB = new JCheckBox("Airplane");
    JCheckBox bCB = new JCheckBox("Boat");
    JCheckBox cCB = new JCheckBox("Clock");


    public mainWindow()
    {
        frame.setLayout(new BorderLayout()); //I know this is set by default to BorderLayout but I just did it when I was out of options to try.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setMinimumSize(new Dimension(360, 480));


        butLabel.setLayout(new GridLayout(1,4));
        cbLabel.setLayout(new GridLayout(2, 2));

        butLabel.add(showBut);
        butLabel.add(exitBut);
        butLabel.add(addBut);
        butLabel.add(remBut);

        cbLabel.add(aCB);
        cbLabel.add(bCB);
        cbLabel.add(cCB);

        frame.add(butLabel, BorderLayout.CENTER);
        frame.add(cbLabel, BorderLayout.NORTH);
    }
    public void setVisible()
    {
        butLabel.setVisible(true);//Didn't think I needed butLabel.setVisible or the cbLabel.setVisible but
        cbLabel.setVisible(true);//again I was trying things that I thought might make sense.

        frame.setVisible(true);
    }
}

1 Answers1

3

do not use Label for grouping elements, use JPanel instead I have tried replace all

Label

with

Panel

it works

maskacovnik
  • 3,080
  • 5
  • 20
  • 26
  • 1
    1+ but also add the reason why to your answer: layout managers, JLabel defaults to null, JPanel to FlowLayout). – Hovercraft Full Of Eels Oct 19 '14 at 19:20
  • Yes, thanks I'll vote in 9 minutes. Should have thought about it more before posting (-_-). Thanks again! – Eric Ashenden Oct 19 '14 at 19:23
  • No, I'm not right, I didn't read the code, since I see that the OP did provide a layout for the JLabel. Likely its due to `getPreferredSize()` returning 0,0 for the JLabel. – Hovercraft Full Of Eels Oct 19 '14 at 19:31
  • I have found this: http://stackoverflow.com/questions/13893613/im-trying-to-set-jtextfield-under-jlabel, read the answer, thats why it returns 0,0, @HovercraftFullOfEels – maskacovnik Oct 19 '14 at 19:36
  • Thanks for the link, but that's not the reason. The reason is as I suspected, JLabel does not use any contained components to calculate its preferredSize but rather uses the sizes of any text and icon added to the label. Please see camickr's answer [here](http://stackoverflow.com/a/8578740/522444) for confirmation. – Hovercraft Full Of Eels Oct 19 '14 at 20:17