Link to home
Start Free TrialLog in
Avatar of BugLighter
BugLighter

asked on

How to hide a pane in a JSplitpane?

working with sdk2 swing 1.1

I have two panels in a jsplitpane left and right. I want to enable the ability
to hide\show each one of them.

I have tried to do that by playing with the location and size of the splitter
but did not get a efficient feel.
Is there another way of doing that?
Avatar of AlexJ030200
AlexJ030200

Hi  BugLighter,

Setting the divider location is the way of doing what you want. Have you tried the following method calls?

setDividerLocation(0.0D)

and

setDividerLocation(1.0D)

Avatar of BugLighter

ASKER

Adjusted points from 200 to 500
Yes I did.
The problem is:
1) Can't get rid of the jsplitpane border therefor the divider is seen even when its size is 0.0 .
2) If you change the Window size the
"hidden" panel is seen (one can overcome
this problem).
These problems made me think that there mast be an elegant solution.
Umm, I've never used a JSplitPane, but won't removing/adding the panels do?
Avatar of Jim Cakalic
JSplitPane.setDividerSize(0) will 'remove' the divider completely.

Are you saying that there are times when you want both components shown and times when you want only one shown?

Jim Cakalic
I think that Sasha is on the right path.  It seems that what you want is some maximized display of the left or right pane, where not even the JSplitPane is visible.  I don't think that this is possible - the JSplitPane will always need some visible component, otherwise you couldn't grow it once you made it 0 size.  Therefore, you will have to have some maximize method which will remove the item from the panel, remove the JSplitPane from your container and replace it with your component.  You'll need a restore method to reverse the process.

Here's an example of what I'm talking about:

---------- SplitPaneDemo.java ----------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class SplitPaneDemo {
    JFrame frame;

    public static void main(String[] args) {
        SplitPaneDemo demo = new SplitPaneDemo();
        demo.makeFrame();
        demo.frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        demo.frame.pack();
        demo.frame.show();
    }


    public JFrame makeFrame() {
        frame = new JFrame();
        // Create a horizontal split pane.
        final JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        JButton showleft = new JButton("Left");
        showleft.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pane.setDividerSize(0);
                pane.setDividerLocation(1.0);
            }
        });
        JButton showright = new JButton("Right");
        showright.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pane.setDividerSize(0);
                pane.setDividerLocation(0.0);
            }
        });
        JButton showboth = new JButton("Both");
        showboth.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pane.setDividerLocation(0.5);
                pane.setDividerSize(10);
            }
        });

        JPanel buttons = new JPanel();
        buttons.setLayout(new GridBagLayout());
        buttons.add(showleft);
        buttons.add(showright);
        buttons.add(showboth);
        frame.getContentPane().add(buttons, BorderLayout.NORTH);

        // Set the minnimum size to 480 by 300.
        pane.setPreferredSize(new Dimension(400, 300));
        // Create our Calendar pane.
        JPanel left = new JPanel();
        left.setBackground(Color.red);
        pane.setLeftComponent(left);
        JPanel right = new JPanel();
        right.setBackground(Color.green);
        pane.setRightComponent(right);
        // Set the divider dead in the middle of the two panes.
        pane.setDividerLocation(0.5);
        pane.setDividerSize(10);
        frame.getContentPane().add(pane, BorderLayout.CENTER);
        return frame;
    }

}
---------- end ----------

Jim

One note on the example I just posted. Event when the divider size is 0, there remains the ability, given very exact pointer positioning, for the user to drag the divider. It's as if it is there, 1 or 2 pixels wide, even though there is no visible presence.

Is JSplitPane really what you want to use? Do you want the user to be able to see both components simultaneously at some times and only one component at other times? What will be the mechanism for the user to indicate this preference?

BTW, I was playing with setResizeWeight to overcome the window resize problems. I'm rapidly coming to the conclusion that JSplitPane just doesn't paint correctly.
Thanks Jim, I might have been unclear but the example you just gave is the start point of my quetion.

Answering your quetions:
1) I think the JSplitPane is the proper
   gadget, do you have any other idea?

2) yes.
3) Use will be able to indicate her preference via toolbar or menubar.

Sasha, Alex can one of you post an example with Jim's SplitPaneDemo working with add\remove panel?

Thanks,
BugLighter.
umm, there's really no example... just use remove(Component) and add(Component) to add and remove the panels from the JSplitPane...
ASKER CERTIFIED SOLUTION
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, BugLighter, one more time removing and adding components.

---------- SplitPaneDemo.java ----------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class SplitPaneDemo {
    JFrame frame;
    JPanel left, right;
    JSplitPane pane;
    int lastDividerLocation = -1;

    public static void main(String[] args) {
        SplitPaneDemo demo = new SplitPaneDemo();
        demo.makeFrame();
        demo.frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        demo.frame.show();
    }


    public JFrame makeFrame() {
        frame = new JFrame();
        // Create a horizontal split pane.
        pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        left = new JPanel();
        left.setBackground(Color.red);
        pane.setLeftComponent(left);
        right = new JPanel();
        right.setBackground(Color.green);
        pane.setRightComponent(right);

        JButton showleft = new JButton("Left");
        showleft.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Container c = frame.getContentPane();
                if (pane.isShowing()) {
                    lastDividerLocation = pane.getDividerLocation();
                }
                c.remove(pane);
                c.remove(left);
                c.remove(right);
                c.add(left, BorderLayout.CENTER);
                c.validate();
                c.repaint();
            }
        });
        JButton showright = new JButton("Right");
        showright.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Container c = frame.getContentPane();
                if (pane.isShowing()) {
                    lastDividerLocation = pane.getDividerLocation();
                }
                c.remove(pane);
                c.remove(left);
                c.remove(right);
                c.add(right, BorderLayout.CENTER);
                c.validate();
                c.repaint();
            }
        });
        JButton showboth = new JButton("Both");
        showboth.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Container c = frame.getContentPane();
                c.remove(pane);
                c.remove(left);
                c.remove(right);
                pane.setLeftComponent(left);
                pane.setRightComponent(right);
                c.add(pane, BorderLayout.CENTER);
                if (lastDividerLocation >= 0) {
                    pane.setDividerLocation(lastDividerLocation);
                }
                c.validate();
                c.repaint();
            }
        });

        JPanel buttons = new JPanel();
        buttons.setLayout(new GridBagLayout());
        buttons.add(showleft);
        buttons.add(showright);
        buttons.add(showboth);
        frame.getContentPane().add(buttons, BorderLayout.NORTH);

        pane.setPreferredSize(new Dimension(400, 300));
        frame.getContentPane().add(pane, BorderLayout.CENTER);
        frame.pack();
        pane.setDividerLocation(0.5);
        return frame;
    }

}
---------- end ----------

Best regards,
Jim Cakalic
Thanks Jim for your time and for the clear examples you kindly provided.

BugLighter.