Below is the code for a simple layout created using several nested JSplitPanes
.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class CDBurner extends JFrame {
private static final long serialVersionUID = -6027473114929970648L;
JSplitPane main, folder, rest;
JPanel centeral, folders, favourites, tasks;
JLabel label;
private CDBurner() {
super("Dan's CD Burner");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 1));
getContentPane().setBackground(Color.black);
createLayout();
pack();
setMinimumSize(getSize());
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
setVisible(true);
requestFocus();
}
private void createLayout() {
createPanels();
rest = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, centeral, tasks);
rest.setResizeWeight(1);
rest.setContinuousLayout(true);
rest.setOneTouchExpandable(true);
folder = new JSplitPane(JSplitPane.VERTICAL_SPLIT, favourites, folders);
folder.setResizeWeight(0.35);
folder.setContinuousLayout(true);
folder.setOneTouchExpandable(true);
main = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, folder, rest);
main.setResizeWeight(0);
main.setContinuousLayout(true);
main.setOneTouchExpandable(true);
getContentPane().add(main);
}
private void createPanels() {
createFolders();
createCenter();
createTaskSpool();
createFavourites();
}
private void createFolders() {
folders = new JPanel(new GridLayout(1, 1));
label = new JLabel("Folder");
folders.setMinimumSize(new Dimension(300, 100));
folders.add(label);
}
private void createCenter() {
centeral = new JPanel(new GridLayout(1, 1));
label = new JLabel("Central");
centeral.add(label);
centeral.setMinimumSize(new Dimension(300, 100));
}
private void createTaskSpool() {
tasks = new JPanel(new GridLayout(1, 1));
label = new JLabel("Task");
tasks.setMinimumSize(new Dimension(300, 100));
tasks.add(label);
}
private void createFavourites() {
favourites = new JPanel(new GridLayout(1, 1));
label = new JLabel("Fav");
favourites.setMinimumSize(new Dimension(300, 100));
favourites.add(label);
}
public static void main(String[] args) {
new CDBurner();
}
}
Due to the line rest.setResizeWeight(1);
you can drag the main (JSplitPane)
divider to the right and it will shrink the tasks JPanel
to the until both the JPanels
in rest
are the minimum size. However, if you try to do the opposite nothing happens. See images below to see problem.
If the gui looks like this, you can drag the main
divider.
And you will get the result of this.
However if it looks like this and you try to drag the rest
divider nothing happens.
This is because both sides of the rest JSplitPane
are already at their minimum size.
The Question
How can I make it so that when I drag the rest
divider it effects the main JSplitPane
so that both the operations shown in the images above are possible?