Re: JSplitPane resize woes
Hi Experts,
I have an issue with my JSpiltPane. Below is the code snippet.
Problem is I have one panel added in Right side of the Spilt Pane, and that Panel has many components like Label,TextArea,Panel,Checkbox etc. And this panel is itself a Titled Border Panel.
When I am maximizing my screen, the Right side panel is not resizing. I tried a lot with the layouts but no Luck.
Would be great if someone can help me on this.
------------------------
import javax.swing.JFrame;
/*
* GridBagLayoutDemo.java requires no other files.
*/
import java.awt.*;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
class SplitPane
extends JFrame
{
private JSplitPane splitPaneV;
private JSplitPane splitPaneH;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
public SplitPane()
{
setTitle( "Split Pane Application" );
//setBackground( Color.gray );
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create the panels
createPanel1();
createPanel2();
//createPanel3();
// Create a splitter pane
// splitPaneV = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
//topPanel.add( splitPaneV, BorderLayout.CENTER );
JPanel main = new JPanel();
main.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.5;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(5, 5, 0, 0);
JPanel acGlobalPlanel = panel2;
main.add(acGlobalPlanel,gbc);
JPanel currentPanel = new JPanel();
currentPanel.setLayout(new BorderLayout());
/*currentPanel.setLayout(new GridBagLayout());
GridBagConstraints g = new GridBagConstraints();
g.anchor = GridBagConstraints.CENTER;
g.fill = GridBagConstraints.BOTH;
g.weightx = 100;
g.weighty = 100;
g.gridx = 0;
g.gridy = 0;*/
splitPaneH = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,panel1,currentPanel );
splitPaneH.setContinuousLayout(true);
splitPaneH.setPreferredSize( new Dimension(600,600));
topPanel.add( splitPaneH, BorderLayout.CENTER );
JPanel curr = (JPanel)splitPaneH.getRightComponent();
curr.add(main, BorderLayout.CENTER);
//currentPanel.add(main, BorderLayout.CENTER);
/*currentPanel.validate();
currentPanel.repaint();*/
Border matteBorder = BorderFactory.createMatteBorder(1, 0, 0, 0,
SystemColor.controlDkShadow);
Border titledBorder = BorderFactory.createTitledBorder(matteBorder,
"Circuit Attributes",
TitledBorder.LEFT,
TitledBorder.ABOVE_TOP);
currentPanel.setBorder(titledBorder);
currentPanel.revalidate();
currentPanel.repaint();
//splitPaneV.setLeftComponent( splitPaneH );
//splitPaneV.setResizeWeight(0.333);
//splitPaneV.setContinuousLayout(true);
//splitPaneV.setOneTouchExpandable(true);
//splitPaneV.setRightComponent( panel3 );
pack();
setLocationRelativeTo(getOwner());
}
public void createPanel1()
{
panel1 = new JPanel();
panel1.setLayout( new GridBagLayout() );
GridBagConstraints gc = new GridBagConstraints();
gc.anchor = GridBagConstraints.CENTER;
gc.fill = GridBagConstraints.VERTICAL;
gc.gridx = 0;
gc.gridy = 0;
gc.insets = new Insets(10, 7, 0, 7);
// Add some buttons
panel1.add( new JButton( "First" ), gc );
gc.gridx = 0;
gc.gridy = 1;
panel1.add( new JButton( "Second" ), gc );
gc.gridx = 0;
gc.gridy = 2;
panel1.add( new JButton( "Third" ), gc );
gc.gridx = 0;
gc.gridy = 3;
panel1.add( new JButton( "Fourth" ), gc );
gc.gridx = 0;
gc.gridy = 4;
panel1.add( new JButton( "Fifth" ), gc );
}
public void createPanel2()
{
panel2 = new JPanel();
panel2.setBorder(new TitledBorder("Global Attributes"));
panel2.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(10, 7, 0, 7);
VpslNameAttr vpslNameAttr = new VpslNameAttr();
panel2.add(vpslNameAttr.getUI(),gbc);
gbc.gridx = 0;
gbc.gridy = 1;
VPNIdAttr vplsDescAttr = new VPNIdAttr();
panel2.add(vplsDescAttr.getUI(),gbc);
}
public void createPanel3()
{
panel3 = new JPanel();
panel3.setLayout( new BorderLayout() );
panel3.setPreferredSize( new Dimension( 600, 400 ) );
panel3.setMinimumSize( new Dimension( 600, 400 ) );
panel3.add( new JLabel( "Notes:" ), BorderLayout.NORTH );
panel3.add( new JTextArea(), BorderLayout.CENTER );
}
public static void main( String args[] )
{
// Create an instance of the test application
SplitPane mainFrame = new SplitPane();
mainFrame.setSize(600, 400);
mainFrame.setVisible( true );
}
}
class VpslNameAttr {
JTextField pwNameTF;
private JPanel p;
VpslNameAttr() {
p = new JPanel();
//p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.setLayout( new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 10000;
gbc.gridheight = 10000;
p.add(new JLabel("Name: "),gbc);
//p.add(new JLabel("Name: "));
pwNameTF = new JTextField();
pwNameTF.setPreferredSize(new Dimension(130, 20));
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 10000;
gbc.gridheight = 10000;
p.add(pwNameTF,gbc);
//p.add(pwNameTF);
}
public JPanel getUI() {
return p;
}
public String getPWName() {
return pwNameTF.getText();
}
}
class VPNIdAttr {
TextField vpnIdAttrTF;
private JPanel p;
VPNIdAttr()
{
p = new JPanel();
//p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 100;
gbc.gridheight = 100;
p.add(new JLabel("VPN ID: "),gbc);
//p.add(new JLabel("VPN ID: "));
vpnIdAttrTF = new TextField(15);
vpnIdAttrTF.setPreferredSize(new Dimension(130, 20));
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 100;
gbc.gridheight = 100;
p.add(vpnIdAttrTF,gbc);
//p.add(vpnIdAttrTF);
}
public JPanel getUI() {
return p;
}
public String getVPNId() {
return vpnIdAttrTF.getText();
}
}
--------------------------------------------------
On Thursday, November 25, 2004 5:41:24 AM UTC+5:30, Larry Coon wrote:
I've reduced my problem to a short, self-contained example,
below. I have a JFrame that has a JSplitPane, that has a
JTree (left) and JTextArea (right). I can't get resize
behavior to work correctly. I've tried many combinations of
setting minimum size & preferred size on each/all of the
components, but nothing did what I wanted. Here's a minimal
example:
import java.awt.*;
import javax.swing.*;
public class SplitPaneDemo extends JFrame {
public SplitPaneDemo() {
super("Split Pane Demo");
Container container = getContentPane();
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent(new JTree());
JTextArea textArea = new JTextArea();
splitPane.setRightComponent(textArea);
textArea.setLineWrap(true);
container.add(splitPane, BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public static void main(String[] args) {
new SplitPaneDemo();
}
}
If I dont setLineWrap(true) then I can move the JSplitPane's
separator bar correctly. As it is here, I can move it left,
but not right. It seems like the JTextArea is resizable
bigger, but not smaller, when LineWrap is true.
My actual program is a lot more involved (the right hand side
is a JTextArea in a JPanel (along with other components) on
a JTabbedField on the right hand side of the JSplitPane), but
I'm obviously missing something basic. What's the CORRECT
way to get the above working correctly?
Thanks.