Solved

How to hide a pane in a JSplitpane?

Posted on 2000-05-04
13
2,946 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
Comment Utility
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
Comment Utility
Adjusted points from 200 to 500
0
 

Author Comment

by:BugLighter
Comment Utility
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
Comment Utility
Umm, I've never used a JSplitPane, but won't removing/adding the panels do?
0
 
LVL 19

Expert Comment

by:Jim Cakalic
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 19

Expert Comment

by:Jim Cakalic
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks Jim for your time and for the clear examples you kindly provided.

BugLighter.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 46
mapAB Challlenge 35 84
Eclipse Neon and jdk 1.8.0 11 64
Eclipse IDE - Cannot copy/paste from console output 8 58
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

772 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

17 Experts available now in Live!

Get 1:1 Help Now