3

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);
ping
  • 1,229
  • 3
  • 21
  • 41

2 Answers2

7

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.

Perry Monschau
  • 883
  • 8
  • 22
  • Spot on. The second panel had nothing inside it. Plus the first panel had no Layout manager too.Thanks. Accepting as answer – ping Mar 18 '12 at 18:16
  • Thank you! I had a similar problem, I changed my layout to a `FlowLayout` and it then my content loaded. – JFreeman Jul 02 '18 at 04:31
2

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.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • if "setting pref size" means you implemented the getPreferedSize of your custom component to return a reasonable sizing hint based on its state, that's fine. If on the other hand, you do a setPrefSize on the component, that's a nononnever (for details see http://stackoverflow.com/a/7229519/203657) – kleopatra Sep 30 '12 at 12:50