Tôi có một JFrame với một JSplitPane đó là OneTouchExpandable. Tôi muốn nhớ vị trí Divider cuối cùng của JSplitPane trên JFrame vứt bỏ và khôi phục Vị trí nếu JFrame được mở lại.Làm thế nào để thiết lập JSplitPane-Divider sụp đổ/mở rộng Nhà nước?
Nó hoạt động tốt, nhưng nếu người dùng mở rộng một bên thông qua giao diện người dùng tiện ích mộtTouchExpandable thì tôi chỉ lưu trữ 'int'-Position khi vứt bỏ và đặt' int'-Position 'trở lại với hậu quả trên JFrame- thay đổi kích thước JSplitPane-Divider nhảy tới thành phần đã thu gọn được ưa thíchSize.
Làm cách nào tôi có thể nhận/đặt trạng thái thu gọn/mở rộng?
EDIT
Bây giờ là: thay đổi kích thước-Hành vi là OK, nhưng nó không phải là chính xác hành vi tương tự như lần đầu mở - nguyên nhân bây giờ tôi không có MinimumDividerLocation. Tôi muốn SnapIn nhưng tiếp tục là collapsedState.
public class SplitPaneState {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SplitPaneState().createAndSowGUI();
}
});
}
private int position = -1;
private Dimension size = new Dimension(500, 300);
private void createAndSowGUI() {
final JFrame frame = new JFrame("frame");
frame.setSize(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.getContentPane().add(new JButton(new AbstractAction(){
{
putValue(Action.NAME, "Open Dialog");
}
@Override
public void actionPerformed(ActionEvent e) {
final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JLabel("left Component"), new JLabel("right Component"));
splitPane.setContinuousLayout(true);
splitPane.setOneTouchExpandable(true);
if(position != -1) {
boolean LeftIsCollapsed = position < splitPane.getMinimumDividerLocation();
if(LeftIsCollapsed) {
splitPane.getLeftComponent().setMinimumSize(new Dimension()); // fix by Martijn Courteaux
splitPane.setDividerLocation(0.0d); // fix by Martijn Courteaux
}else {
splitPane.setDividerLocation(position);
}
}
JDialog dialog = new JDialog(frame,"dialog"){
@Override
public void dispose() {
position = splitPane.getDividerLocation();
size = this.getSize();
super.dispose();
}
};
dialog.setSize(size);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(frame);
dialog.getContentPane().add(splitPane);
dialog.setVisible(true);
}
}
));
frame.setVisible(true);
}
}
yeah, thay đổi kích cỡ-Hành vi là OK, nhưng nó không phải là chính xác hành vi tương tự như lần đầu tiên mở - nguyên nhân bây giờ tôi không có MinimumDividerLocation. Tôi muốn SnapIn nhưng tiếp tục là collapsedState. – oliholz
Sau khi thử các giải pháp đề xuất khác nhau, tôi tìm thấy giải pháp sau đây để làm việc: '' pane.getRightComponent(). SetMinimumSize (new Dimension()); pane.setResizeWeight (1.0d); '' –
Điều đó làm việc cho tôi (cảm ơn! :)) nhưng vị trí chia không dính khi thay đổi kích thước, đặc biệt là khi bạn không thể ghi đè kích thước tối thiểu thành phần. Xem câu trả lời của tôi để hack nó nếu bạn sử dụng 'BasicSplitPaneUI'. – Matthieu