Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding components to BoxLayout dynamically  RSS feed

 
Josh Sandeman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I am developing using Java 5 under JBuilder. I have a JPanel that uses a BoxLayout. Upon construction it correctly adds and displays components, but my application sometimes need to adds components to the panel (and thus to the BoxLayout) after it is already built and displayed. Here's the code snippet:

ActionWidget newWidget = new ActionWidget(action.getActionName(), ImageFactory.createImageIcon("images/M_red.gif", ""));
JPanel newPanel = new JPanel(new FlowLayout());
newPanel.add(newWidget);
actionWidgetPanels.add(newPanel);
layout.add(newPanel);
layout.add(Box.createVerticalStrut(strutHeight));

When I step through the code, everything seems to get created and added, but the display never updates to show the new components. I tried explicit calls to repaint() and invalidate() all to no avail....
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try
1)
validate();
repaint();//sometimes this is not required - test with, then without

2)
[boxlayout_panel].revalidate();
 
Josh Sandeman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither of these worked. I have since chucked the layout manager altogether because it simply doesn't meet my needs and am using absolute positioning. The solutions you suggested don't worked here, either, although interestingly enough the display DOES refresh properly sometimes, but not others. It seems random as far as I can tell....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!