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); 
4
  • 2
    Please post the code where you are adding above panel.. Commented Mar 18, 2012 at 12:50
  • 1
    For better help sooner, post an SSCCE. Commented Mar 18, 2012 at 12:55
  • 2
    Ok, System.out your second panel's location and size and lemme know the result. Commented Mar 18, 2012 at 13:00
  • The size and Locations turned out as -- java.awt.Dimension[width=0,height=0] java.awt.Point[x=0,y=0] respectively. This looks like the issue. How do i add this panel to my first panel without having to mention any fixed size ? Commented Mar 18, 2012 at 13:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Spot on. The second panel had nothing inside it. Plus the first panel had no Layout manager too.Thanks. Accepting as answer
Thank you! I had a similar problem, I changed my layout to a FlowLayout and it then my content loaded.
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.

1 Comment

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 stackoverflow.com/a/7229519/203657)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.