简体   繁体   English

动态更改嵌套的 Swing 组件

[英]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 .这是我的问题:我有一个JPanel ,其中包含一个JTabbedPane ,它有一个使用自定义JPanelJScrollPane Quite simply, I change customized JPanel a ways into my program, and I need all of the parent components to reflect the change.很简单,我将自定义 JPanel 更改为我的程序的一种方式,并且我需要所有父组件来反映更改。 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 .其中infoScollerJScrollPaneinfoPanel是自定义的JPanel

It should go from saying | Hi! |应该是 go 从说| Hi! | | Hi! | (or something like that, you get the idea) to saying | Bye! | (或类似的东西,你明白了)说| Bye! | | Bye! | but instead it goes from | Hi! |但它来自| Hi! | | 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.编辑:通过自定义,我的意思是它扩展了 JPanel,并具有一些组件等。 reevaluateInfoPane() 根据新信息将 infoPanel 设置为等于新的 infoPanel。 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...我知道 reevalutateInfoPanel() 可以满足我的要求,因为我可以说它是一个新的 JFrame 并且它是正确的......无论如何这里是代码......这可能会比帮助更令人困惑,因为它真的没有必要...

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 .更新:我发现问题出在JTabbedPane的某个地方,它没有呈现更新的JScrollPane Instead, it just removes it altogether when I revalidate and repaint it (the JTabbedPane ).相反,当我revalidaterepaint它( JTabbedPane )时,它只是将它完全删除。

You should call revalidate() and repaint() on the container you changed.您应该在您更改的容器上调用revalidate()repaint() From the description, it sounds like you should call them on infoPanel .从描述中,听起来您应该在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 .看看您如何更改infoPanel的内容也许会有所帮助。

As already was suggested, calling revalidate() and repaint() on the parent component will be enough.正如已经建议的那样,在父组件上调用revalidate()repaint()就足够了。 It automatically propagates revalidation and repainting to all components inside that component.它会自动将重新验证和重新绘制传播到该组件内的所有组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM