0

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).

MirroredFate
  • 12,396
  • 14
  • 68
  • 100

2 Answers2

0

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.

jackrabbit
  • 5,525
  • 1
  • 27
  • 38
  • AFAIK, just setting the viewport view of the scroll pane should be sufficient. revalidate and repaint shouldn't be necessary. – JB Nizet Jul 12 '11 at 18:20
  • The generic way to make Swing rethink the layout of the component hierarchy and show the changes is as I described. The point is that the 2 methods should be called on infoPanel rather than infoScroller. – jackrabbit Jul 12 '11 at 18:30
  • Generally yes, but setViewPortView calls viewPort.setView, which calls these methods at the end. It's encapsulated in the component. – JB Nizet Jul 12 '11 at 19:13
  • I tried using both of those methods on the infoPanel, and I tried just setting the viewportView without changing anything else... Neither worked. – MirroredFate Jul 12 '11 at 20:05
  • It was not clear to me from the documentation of setViewportView that it calls the two methods. No surprise that my suggestion does not work then. Also, it was not clear to me that you create an entirely new infoPane in reevaluateInfoPanel. Have you tried setMinimumSize on the infoScroller? Grasping at straws here... – jackrabbit Jul 12 '11 at 21:47
0

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.

dhblah
  • 9,751
  • 12
  • 56
  • 92