how can you programmatically set the JSplitPane to hide the right/bottom component when OneTouchExpandable is set to true?

前端 未结 5 1115
遥遥无期
遥遥无期 2021-01-06 06:30

In a JSplitPane, you have the setOneTouchExpandable method which provides you with 2 buttons to quickly fully hide or full show the JSplitPan

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-06 06:44

    import javax.swing.*;
    
    class SplitPaneDefault {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JSplitPane sp = new JSplitPane(
                        JSplitPane.HORIZONTAL_SPLIT,
                        new JTree(),
                        new JTree());
                    sp.setOneTouchExpandable(true);
                    sp.setDividerLocation(0.0);
                    JOptionPane.showMessageDialog(null, sp);
                }
            });
        }
    }
    

    replace 0.0 with 1.0 and you get my problem

    Read the fine manual and solve the problem.

    This method immediately changes the size of the split pane based on its current size. If the split pane is not correctly realized and on screen, this method will have no effect ...

    SplitPaneDefault

    import javax.swing.*;
    
    class SplitPaneDefault {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JSplitPane sp = new JSplitPane(
                        JSplitPane.HORIZONTAL_SPLIT,
                        new JTree(),
                        new JTree());
                    sp.setOneTouchExpandable(true);
                    JFrame f = new JFrame("Split Pane To Right");
                    f.add(sp);
                    f.pack();
                    // sp now has a non-zero size!
                    sp.setDividerLocation(1.0);
                    f.setLocationByPlatform(true);
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setVisible(true);
                }
            });
        }
    }
    

提交回复
热议问题