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?
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?
ASKER
Adjusted points from 200 to 500
ASKER
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.
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?
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
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.addWindowListen er(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.HORI ZONTAL_SPL IT);
JButton showleft = new JButton("Left");
showleft.addActionListener (new ActionListener() {
public void actionPerformed(ActionEven t e) {
pane.setDividerSize(0);
pane.setDividerLocation(1. 0);
}
});
JButton showright = new JButton("Right");
showright.addActionListene r(new ActionListener() {
public void actionPerformed(ActionEven t e) {
pane.setDividerSize(0);
pane.setDividerLocation(0. 0);
}
});
JButton showboth = new JButton("Both");
showboth.addActionListener (new ActionListener() {
public void actionPerformed(ActionEven t 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.r ed);
pane.setLeftComponent(left );
JPanel right = new JPanel();
right.setBackground(Color. green);
pane.setRightComponent(rig ht);
// 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
---------- 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.addWindowListen
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.HORI
JButton showleft = new JButton("Left");
showleft.addActionListener
public void actionPerformed(ActionEven
pane.setDividerSize(0);
pane.setDividerLocation(1.
}
});
JButton showright = new JButton("Right");
showright.addActionListene
public void actionPerformed(ActionEven
pane.setDividerSize(0);
pane.setDividerLocation(0.
}
});
JButton showboth = new JButton("Both");
showboth.addActionListener
public void actionPerformed(ActionEven
pane.setDividerLocation(0.
pane.setDividerSize(10);
}
});
JPanel buttons = new JPanel();
buttons.setLayout(new GridBagLayout());
buttons.add(showleft);
buttons.add(showright);
buttons.add(showboth);
frame.getContentPane().add
// 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.r
pane.setLeftComponent(left
JPanel right = new JPanel();
right.setBackground(Color.
pane.setRightComponent(rig
// Set the divider dead in the middle of the two panes.
pane.setDividerLocation(0.
pane.setDividerSize(10);
frame.getContentPane().add
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.
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.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.addWindowListen er(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.HORI ZONTAL_SPL IT);
left = new JPanel();
left.setBackground(Color.r ed);
pane.setLeftComponent(left );
right = new JPanel();
right.setBackground(Color. green);
pane.setRightComponent(rig ht);
JButton showleft = new JButton("Left");
showleft.addActionListener (new ActionListener() {
public void actionPerformed(ActionEven t 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.addActionListene r(new ActionListener() {
public void actionPerformed(ActionEven t 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(ActionEven t e) {
Container c = frame.getContentPane();
c.remove(pane);
c.remove(left);
c.remove(right);
pane.setLeftComponent(left );
pane.setRightComponent(rig ht);
c.add(pane, BorderLayout.CENTER);
if (lastDividerLocation >= 0) {
pane.setDividerLocation(la stDividerL ocation);
}
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
---------- 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.addWindowListen
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.HORI
left = new JPanel();
left.setBackground(Color.r
pane.setLeftComponent(left
right = new JPanel();
right.setBackground(Color.
pane.setRightComponent(rig
JButton showleft = new JButton("Left");
showleft.addActionListener
public void actionPerformed(ActionEven
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.addActionListene
public void actionPerformed(ActionEven
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
public void actionPerformed(ActionEven
Container c = frame.getContentPane();
c.remove(pane);
c.remove(left);
c.remove(right);
pane.setLeftComponent(left
pane.setRightComponent(rig
c.add(pane, BorderLayout.CENTER);
if (lastDividerLocation >= 0) {
pane.setDividerLocation(la
}
c.validate();
c.repaint();
}
});
JPanel buttons = new JPanel();
buttons.setLayout(new GridBagLayout());
buttons.add(showleft);
buttons.add(showright);
buttons.add(showboth);
frame.getContentPane().add
pane.setPreferredSize(new Dimension(400, 300));
frame.getContentPane().add
frame.pack();
pane.setDividerLocation(0.
return frame;
}
}
---------- end ----------
Best regards,
Jim Cakalic
ASKER
Thanks Jim for your time and for the clear examples you kindly provided.
BugLighter.
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)