Solved

How to hide a pane in a JSplitpane?

Posted on 2000-05-04
13
2,983 Views
Last Modified: 2008-03-10
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?
0
Comment
Question by:BugLighter
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 2

Expert Comment

by:AlexJ030200
ID: 2776899
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)

0
 

Author Comment

by:BugLighter
ID: 2776943
Adjusted points from 200 to 500
0
 

Author Comment

by:BugLighter
ID: 2776944
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.
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2777335
Umm, I've never used a JSplitPane, but won't removing/adding the panels do?
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2777809
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
0
 
LVL 3

Expert Comment

by:kostello
ID: 2777822
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.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2777893
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

0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2777920
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.
0
 

Author Comment

by:BugLighter
ID: 2779777
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.
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2780516
umm, there's really no example... just use remove(Component) and add(Component) to add and remove the panels from the JSplitPane...
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 500 total points
ID: 2781288
Try this.

---------- 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.show();
    }


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

        JButton showleft = new JButton("Left");
        showleft.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pane.setLeftComponent(left);
                pane.setRightComponent(null);
                pane.setDividerSize(0);
                pane.setDividerLocation(1.0);
                pane.setResizeWeight(1.0);
            }
        });
        JButton showright = new JButton("Right");
        showright.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pane.setLeftComponent(null);
                pane.setRightComponent(right);
                pane.setDividerSize(0);
                pane.setDividerLocation(0.0);
                pane.setResizeWeight(0.0);
            }
        });
        JButton showboth = new JButton("Both");
        showboth.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pane.setLeftComponent(left);
                pane.setRightComponent(right);
                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);

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


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

Jim
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2782302
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
0
 

Author Comment

by:BugLighter
ID: 2785549
Thanks Jim for your time and for the clear examples you kindly provided.

BugLighter.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 87
splitOdd10 challenge 5 81
Java and GPO 11 73
Java Server Faces parameter pass? 6 39
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now