Solved

How to hide a pane in a JSplitpane?

Posted on 2000-05-04
13
3,095 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

739 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