Dynamically change nested Swing component
Here is my problem: I have a JPanel
that contains a JTabbedPane
which has a JScrollPane
using a customized JPanel开发者_如何学编程
. Quite simply, I change customized JPanel a ways into my program, and I need all of the parent components to reflect the change. However, I have no idea what I need to use in order to accomplish this. This is what I have now:
infoScroller.remove(infoPanel);
this.reevaluateInfoPanel();
infoScroller.setViewportView(infoPanel);
infoScroller.revalidate();
infoScroller.repaint();
where infoScoller
is the JScrollPane
and infoPanel
is the customized JPanel
.
It should go from saying | Hi! |
(or something like that, you get the idea) to saying | Bye! |
but instead it goes from | Hi! |
to | |
EDIT: By customized all I mean is that it extends JPanel, and has some components, etc. reevaluateInfoPane() sets the infoPanel equal to a new infoPanel based upon new information. I know that the reevalutateInfoPanel() does what I want, as I can put it is a new JFrame and it is correct... here is the code anyway... this will probably confuse more than help, as it is really not necessary...
private void reevaluateInfoPanel(){
infoPanel = new JPanel();
GridLayout gl = new GridLayout(infoNum, 1);
infoPanel.setLayout(gl);
//Create a display panel for every info block available
DisplayPanel[] panels = new InfoViewer[infoList.length];
for(int i = 0; i < panels.length; i++){
panels[i] = new InfoViewer(infoList[i]);
}
//Assign a new SelectionPanel for every info selection necessary
for(int i = 0; i < infoNum; i++){
infoPanel.add(new SelectionPanel(panels, "info"));
}
}
UPDATE: I have figured out that the problem lies somewhere with the JTabbedPane
, which is not rendering the updated JScrollPane
. Instead, it just removes it altogether when I revalidate
and repaint
it (the JTabbedPane
).
You should call revalidate()
and repaint()
on the container you changed. From the description, it sounds like you should call them on infoPanel
. Also, make sure your calls happen on the Event Dispatch Thread.
Perhaps it would be helpful to also see how you change the content of infoPanel
.
As already was suggested, calling revalidate()
and repaint()
on the parent component will be enough. It automatically propagates revalidation and repainting to all components inside that component.
精彩评论