Code
JSplitPane sp = new JSplitPane();
sp.setLeftComponent(new JScrollPane(new JTree()));
sp.setRightComponent(new JScrollPane(new JTable(6, 3)));
sp.setResizeWeight(.4);
JButton button = new JButton("swap");
button.setFocusable(false);
button.addActionListener(e -> {
Component left = sp.getLeftComponent();
Component right = sp.getRightComponent();
// sp.removeAll(); // Divider is also removed
sp.remove(left);
sp.remove(right);
// or: https://stackoverflow.com/questions/4871874/java-problem-with-jsplitpane
// sp.setLeftComponent(null);
// sp.setRightComponent(null);
sp.setLeftComponent(right);
sp.setRightComponent(left);
sp.setResizeWeight(1d - sp.getResizeWeight());
if (check.isSelected()) {
sp.setDividerLocation(sp.getDividerLocation());
}
});
References