JSplitPane组件的动态变化过程
来源:爱站网时间:2022-04-09编辑:网友分享
爱站技术频道小编今天来给大家详细说说关于JSplitPane组件的动态变化过程是怎样的?你如果想彻底了解清楚的话,可以看看小编整理的资料,本文仅供参考。
问题描述
我正在创建一个动态变化的摆动框架。这是当前的布局(我正在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
时,没有任何反应。
思路:
以下代码对我有用。我只是删除了旧的孩子,并添加了新的孩子,记住分频器的位置并对其进行了重置。我不知道是否需要重新验证/重新绘制,但是我添加了它是为了很好。
/**
* 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();
}
上述文章就是JSplitPane组件的动态变化过程的全部内容了,希望这个知识点能帮助到你解决问题。想要获取更多有趣的技术文章,来关注我们网站即可。