我正在创建一个动态变化的秋千框架。 这是当前的布局(我在IntelliJ IDEA中工作)
正如您所看到的,主JPanel
有一个JSplitPane
。在JSplitPane
内JTree
(左)和JTable
(右)就在那里。
因此,当我点击JTree
的根节点时,我想将JTable
替换为Editor
,当点击子节点时,该表将显示在那里。
public class SessionWindow {
private JFrame mFrame;
private JPanel container;
private JTree tableTree;
private JTable tableDetails;
private JSplitPane jSplitPane;
private JScrollPane jScrollTree;
private JScrollPane jScrollTable;
private JPanel editorPanel;
private Project mProject;
private Disposable parent;
private ClientSocket socket;
public SessionWindow(Project mProject, Disposable parent, ClientSocket socket) {
super();
this.mProject = mProject;
this.parent = parent;
this.socket = socket;
mFrame = new JFrame();
mFrame.setSize(new Dimension(1024, 500));
mFrame.setResizable(true);
mFrame.setTitle("Debug window");
mFrame.setLayout(new BorderLayout());
mFrame.add(container, BorderLayout.CENTER);
mFrame.pack();
mFrame.setVisible(true);
jSplitPane.setResizeWeight(.4f);
populateDatabase();
tableTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tableTree.addTreeSelectionListener(e -> {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
tableTree.getLastSelectedPathComponent();
/* if nothing is selected */
if (node == null) return;
IBaseModel model = (IBaseModel) node.getUserObject();
if (model instanceof DTable) {
showTablePane();
DTable table = (DTable) model;
populateTable(table);
} else if (model instanceof DDatabase) {
showEditorPane();
}
});
}
private void populateDatabase() {
socket.requestDbDetails();
DataObserver.getInstance().subscribe(listener);
}
private void showTablePane() {
jSplitPane.setRightComponent(null);
jSplitPane.setRightComponent(jScrollTree);
jSplitPane.invalidate();
}
private void showEditorPane() {
editorPanel = new JPanel();
editorPanel.add(createEditor().getComponent(), BorderLayout.CENTER);
jSplitPane.setRightComponent(null);
jSplitPane.setRightComponent(editorPanel);
jSplitPane.invalidate();
}
private Editor createEditor() {
//create an return editor
return editor;
}
}
问题是当我用editor
替换表格时,editor
作为正确的组件(不完全可见,但我们可以在那里看到)
但是在尝试将editor
替换为table
时,什么都不会发生。
答案 0 :(得分:0)
以下代码对我有用。我只是删除了旧的孩子,并添加了新的孩子,记住分频器的位置并对其进行了重置。我不知道是否需要重新验证/重新绘制,但是我添加了它是为了很好。
/**
* replace a child of a splitPane with another
* @param oldChild
* @param newChild
*/
public void replaceSplitPaneChild(JComponent oldChild, JComponent newChild) {
JSplitPane parent = (JSplitPane) oldChild.getParent();
int dividerLocation = parent.getDividerLocation();
parent.remove(oldChild);
parent.add(newChild);
parent.setDividerLocation(dividerLocation);
newChild.revalidate();
newChild.repaint();
}