I use two JSplitPanes
to display some information. One is oriented with a horizontal divider, the other with a vertical divider. The following issue applies to both JSplitPanes
.
When I resize my application's main JFrame
, the JSplitPanes
also resize as they have to occupy a certain percentage of the screen (all JPanels
adjust to JFrame
resize, behavior set by MigLayout
's width and height parameters).
The divider location is important and needs to be constant with the JSplitPane
's size. I use setResizeWeight()
and setDividerLocation()
to achieve this.
The main issue is that on JFrame
resize, the divider does not remain at 0.0/ 0% but instead moves to ~5-10%:
----->
I thought this is due to the resizeWeight
but I played around with it and there was no change in behavior at all.
Can anybody help me out with this problem? I implemented a ComponentAdapter
but short of saving the divider's location in a variable or Preferences
XML-key I cannot restore the divider's location on componentResized()
.
Example code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import net.miginfocom.swing.MigLayout;
public class JSplitPaneTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(500, 500));
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new MigLayout());
JPanel panel1 = new JPanel();
panel1.setBackground(Color.RED);
JPanel panel2 = new JPanel();
panel1.setBackground(Color.GREEN);
final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
panel1, panel2);
splitPane.setResizeWeight(0.5);
splitPane.setOpaque(false);
splitPane.setOneTouchExpandable(true); // does not work on Linux :(
splitPane.setBorder(null);
mainPanel.add(splitPane, "w 100%, h 90%, wrap");
JButton button = new JButton("Set to 0");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
splitPane.setDividerLocation(0.0);
}
});
mainPanel.add(button, "span");
frame.add(mainPanel);
frame.setVisible(true);
}
}