如何设置JSplitPane

如何设置JSplitPane

本文介绍了如何设置JSplitPane-Divider折叠/展开状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有JSplitPane的JFrame,它是OneTouchExpandable。
我想记住JFlitPane在JFrame上的最后一个Divider位置处理并在重新打开JFrame时恢复Position。

I have a JFrame with a JSplitPane that is OneTouchExpandable.I want to remember the last Divider position of the JSplitPane on JFrame dispose and restore the Position if the JFrame is reopened.

它运作良好,但是如果用户通过oneTouchExpandable UI-Widget扩展一侧,那么
我只存储处置和设置的'int'-Position 'int'-Position再次返回JFrame的结果 - 调整JSplitPane-Divider的大小跳转到折叠的Component preferredSize。

It works well, but if the User expand one Side via the oneTouchExpandable UI-Widget thenI store only the 'int'-Position on dispose and set the 'int'-Position back again with the consequence on JFrame-resizing the JSplitPane-Divider jumps to the collapsed Component preferredSize.

我如何获得/设置崩溃/展开状态?

How can I get/set the collapse/expand State?

编辑

现在:调整大小行为没问题,但它与第一​​次打开的行为并不完全相同 - 因为现在我没有MinimumDividerLocation。我想要SnapIn但更多的是collapsedState。

Now: the resize-Behavior is OK, but it is not exactly the same behavior like the first-time-open - cause now I have no MinimumDividerLocation. I wanted the SnapIn but further the 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 );
    }
}


推荐答案

我发现可以通过将组件的最小大小设置为 new Dimension()来折叠splitpane的一侧,然后设置分隔符位置:

I found that it is possible to collapse one side of the splitpane by setting the minimum size of the component to new Dimension() and then set the divider location:

// Hide left or top
pane.getLeftComponent().setMinimumSize(new Dimension());
pane.setDividerLocation(0.0d);

// Hide right or bottom
pane.getRightComponent().setMinimumSize(new Dimension());
pane.setDividerLocation(1.0d);

您可以使用这些设置来存储和恢复崩溃/展开状态。

You can play with these settings to store and restore the collapse/expand state.

这篇关于如何设置JSplitPane-Divider折叠/展开状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:31