我目前正在使用Netbeans GUI构建器创建一个java swing GUI.我有一堆面板正在我的主JFrame上被替换,因为用户导航GUI并且控制器类正在处理这个问题.但是,有一个步骤(FilterDefinitionPanel)包含一个组合框以及一个空白内部面板(QueryHelperPanel).
我想要做的是根据用户在组合框中选择的内容将这个内部面板替换为我创建的另一个内部面板(StringQueryDefinitionPanel).现在,在我的组合框的ComboBoxItemStateChanged事件处理程序下,我的控制器类运行此方法:
public void selectFilterAttribute(Object item) {
/**
* Determine panel to create based on item selection. Currently always returns the same
* StringQueryDefinitionPanel.
*/
JPanel panel = this.getRequiredQueryHelperPanel(item);
/**
* Swap the placeholder QueryHelperPanel with the required one.
*/
((FilterDefinitionPanel) this.mainFrame.getMainPanel()).setQueryHelperPanel(panel);
/**
* Not sure if all of these are needed :\
*/
mainFrame.validate();
mainFrame.repaint();
mainFrame.pack();
}
Run Code Online (Sandbox Code Playgroud)
这就是FilterDefinitionPanel的setQueryHelper方法中发生的事情:
public void setQueryHelperPanel(JPanel panel){
this.remove(queryHelperPanel);
this.queryHelperPanel=panel;
this.queryHelperPanel.repaint();
/**
* Again, not sure which refresh methods are needed...
*/ …Run Code Online (Sandbox Code Playgroud)