Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Panel doesn't refresh when new component added if it's smaller than the largest visible component.  RSS feed

 
David Tong
Greenhorn
Posts: 6
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JPanel that's using a simple GridBagLayout.

JPanel panel = new JPanel();
GridBagLayout qPanelLayout = new GridBagLayout();
qPanelLayout.columnWidths = new int[] { 0 };
qPanelLayout.rowHeights = new int[] { 0 };
qPanelLayout.columnWeights = new double[] { Double.MIN_VALUE };
qPanelLayout.rowWeights = new double[] { 0.0 };
panel.setLayout(qPanelLayout);
componentCount = 0;

Based on user input I am adding sub-panels to the panel. These sub-panels may vary in height. They take up the full width of the Panel.

public void add(Component comp) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(2,2,2,2);
gbc.gridx = 0;
gbc.gridy = componentCount++;
panel.add(comp, gbc_questionPane1);
}

The odd behaviour I'm seeing is that if the height of the new sub-panel I'm adding is less than the height of the largest sub-panel currently displayed
then the main panel does not repaint. If its height is equal or greater then the Panel does repaint, and any sub-panels that were added but not visible before are now painted.
What have I missed?
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An intriguing question, but I don't think you've given us enough info for an assessment of what might be going on. Knowing whether you call repaint() and/or revalidate() after adding components and when would be helpful.

On the whole, adding components to a container while the Swing application is running has always been challenging. It can be done, even reliably I think, but mechanisms were provided to avoid having to hassle with it. For example, to do what you describe I recommend using CardLayout with at least 2 different 'cards.' One will be visible at all times while the other is being built in response to user input. Then, when the the newly built card is ready, it is made visible, replacing the currently visible card.
 
David Tong
Greenhorn
Posts: 6
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the solution was more bizarre than the problem. I was wrong about it depending on the height of the visible components.
In fact the panels that caused the refresh to occur correctly all had a JComboBox somewhere.
Panels that didn't display initially did not have a JComboBox - they had JLabels and JButtons.

Adding a dummy JComboBox and setting its visibility to False resolved the problem. I haven't figured out why;
perhaps there's a listener that the JComboBox registers which is triggering it.

Well that's the weirdest bug I've hit recently.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

How do you know that is a bug? It all depends how you are calling the add method, and when you set the GUI visible.
 
David Tong
Greenhorn
Posts: 6
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's certainly a bug in my code; I never claimed it was a bug in Java.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaaaaaaaaaaaa! I see. Thank you for explaining.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!