ping
ping

Reputation: 1239

Java swing --JPanel on JPanel not showing up

I trying to add a JPanel to another JPanel but am faced with the problem that the second Jpanel will not show up on the first one.
My basic structure of things is as follows --
I have a JPanel panel1 which has a BoxLayout and by virtue of HorizontalBoxes and VerticalBoxes i keep adding JComponents to it. All JComponents appear on panel1 except for the second JPanel. The code for the second JPanel which wont appear is as follows --

public class LabelMacroEditor extends JPanel implements PropertyChangeListener {

    private static final long serialVersionUID = 1L;
    private LabelMacroModel model;

    public LabelMacroEditor(LabelMacroModel bean) {

        this.model = bean;
        model.addPropertyChangeListener(this);
        setupComponents();
        validate();
        setVisible(true);
    }

    public void setupComponents()
    {
        Box allButtons =  Box.createVerticalBox();
        for(MacroModel macroModel : model.getMacroModelList())
        {
            LabelMacroEditorEditableEntity macroEditorEntity =  new LabelMacroEditorEditableEntity(macroModel);
            Box entityBox =  Box.createHorizontalBox();
            entityBox.add(macroEditorEntity.getUpButton());
            entityBox.add(Box.createHorizontalStrut(15));
            entityBox.add(macroEditorEntity.getMacroDetailsButton());
            entityBox.add(Box.createHorizontalStrut(15));
            entityBox.add(macroEditorEntity.getDownButton());

            allButtons.add(entityBox);
        }

        add(allButtons);
    }

    @Override
    public void propertyChange(PropertyChangeEvent arg0) {
        revalidate();
    }

}

I have tested LabelMacroEditor in a standalone way by adding it to a JFrame and found that it appears fine. Im assuming its has something to do with come confliction revalidate/setVisible or the like.
Am i missing something obvious ?

I can post more code from the JPanel that is adding LabelMacroEditor if there is a need.

EDIT : The code snippet from where im adding LabelMacroEditor is as follows --

private final LabelMacroModel labelMacroModel;
private LabelMacroEditor labelMacroEditor;
//code to populate labelMacroModel
Box verticalBox  = Box.createVerticalBox();
// Add all other JComponents to verticalBox
labelMacroEditor = new LabelMacroEditor(labelMacroModel);
verticalBox.add(labelMacroEditor);
add(verticalBox);

Upvotes: 3

Views: 6305

Answers (2)

SpringLayout
SpringLayout

Reputation: 21

I have the same problem:

  • I use layouts
  • I tried with TextArea as well ... and nothing.
  • It is showing fine with JFrame
  • Other components are displayed normally (which are basic swing components)

I fix this by setting Preferred size to the custom panel/component.

Upvotes: 2

Perry Monschau
Perry Monschau

Reputation: 901

I recon it's either that your first panel doesn't have a layout manager, in which case you'll need to use setLayout();

or

it's because the second panel has nothing inside it and so it's preferred size is 0. Try adding a new JTextArea(10,5); to it and see what happens.

Upvotes: 7

Related Questions