Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JSplitPane resize woes

880 views
Skip to first unread message

Larry Coon

unread,
Nov 24, 2004, 7:11:24 PM11/24/04
to
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.

Larry Coon

unread,
Nov 24, 2004, 9:23:49 PM11/24/04
to
Larry Coon wrote:

(snipped)

As a supplement to my last post, I can ALMOST get it to work the
way I want like this:

package larry.sybasetexteditor;

import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;

public class SplitPaneDemo extends JFrame {
public SplitPaneDemo() {
super("Split Pane Demo");

Container container = getContentPane();

JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
for (int i = 1; i <= 50; i++)
root.add(new DefaultMutableTreeNode("Node " + i));

JTree tree = new JTree(root);
tree.setPreferredSize(new Dimension(200, 500));

splitPane.setLeftComponent(new JScrollPane(tree));

JTextArea textArea = new JTextArea();

textArea.setLineWrap(true);
splitPane.setRightComponent(new JScrollPane(textArea));

container.add(splitPane, BorderLayout.CENTER);

setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}

public static void main(String[] args) {
new SplitPaneDemo();
}
}

The only problem with this is that it no longer vertically
scrolls through the JTree correctly (note that there are 50
nodes in the tree, but you can't scroll down to see them
all). Any suggestions?

Thanks.

John McGrath

unread,
Nov 27, 2004, 1:50:26 AM11/27/04
to
On 11/24/2004 at 9:23:49 PM, Larry Coon wrote:

> Larry Coon wrote:
>
> (snipped)
>
> As a supplement to my last post, I can ALMOST get it to work the
> way I want like this:

:
> tree.setPreferredSize(new Dimension(200, 500));

Try setting the minimum size rather than the preferred size. If you want
the JSplitPane to respect that minimum size, set the minimum size of the
component that you place in the JSplitPane, i.e. the JScrollPane(). Note
that you will probably want to choose a smaller height value.

--
Regards,

John McGrath

Larry Coon

unread,
Nov 29, 2004, 3:35:16 PM11/29/04
to
John McGrath wrote:

> Try setting the minimum size rather than the preferred size. If you want
> the JSplitPane to respect that minimum size, set the minimum size of the
> component that you place in the JSplitPane, i.e. the JScrollPane(). Note
> that you will probably want to choose a smaller height value.

Thanks for the reply, John. After a lot more mucking around with it,
it turned out that setting the preferred size of the JScrollPane did
the trick.

vicky...@gmail.com

unread,
Apr 3, 2013, 1:55:09 AM4/3/13
to lmc...@nospam_cox.net
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();
}
}

--------------------------------------------------

John B. Matthews

unread,
Apr 3, 2013, 5:57:49 PM4/3/13
to
In article <677c5cd9-ccee-413e...@googlegroups.com>,
vicky...@gmail.com wrote:

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

I've come to like GroupLayout for labeled fields, as shown
below and here <http://stackoverflow.com/a/8504753/230513>.

A few other points:

Use EventQueue.invokeLater() to construct your GUI on the EDT.

Don't extend top-level containers needlessly.

Don't set a component's size; let it adopt its preferred size or
the size prescribed by the enclosing container's layout.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

public class SplitPane {

private JPanel mainPanel = new JPanel(new BorderLayout());
private Box left = createLeft();
private JPanel right = createRight();

public SplitPane() {
JSplitPane splitPane = new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT, left, right);
splitPane.setContinuousLayout(true);
mainPanel.add(splitPane, BorderLayout.CENTER);

JPanel panel = new JPanel();
Border matteBorder = BorderFactory.createMatteBorder(
1, 0, 0, 0, SystemColor.controlDkShadow);
Border titledBorder = BorderFactory.createTitledBorder(matteBorder,
"Circuit Attributes", TitledBorder.LEFT, TitledBorder.ABOVE_TOP);
panel.setBorder(titledBorder);
mainPanel.add(panel, BorderLayout.NORTH);
}

private Box createLeft() {
Box box = new Box(BoxLayout.Y_AXIS);
box.add(new JButton("First"));
box.add(new JButton("Second"));
box.add(new JButton("Third"));
box.add(new JButton("Fourth"));
box.add(new JButton("Fifth"));
box.add(Box.createVerticalGlue());
return box;
}

private JPanel createRight() {
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder("Global Attributes"));
JLabel label1 = new JLabel("Name:");
JTextField field1 = new JTextField(15);
JLabel label2 = new JLabel("VPN ID:");
JTextField field2 = new JTextField(15);
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(label1)
.addComponent(label2))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(field1)
.addComponent(field2)));
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label1)
.addComponent(field1))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label2)
.addComponent(field2)));
return panel;
}

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("Split Pane Application");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SplitPane sp = new SplitPane();
f.add(sp.mainPanel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

David Hilton

unread,
Sep 5, 2020, 2:00:48 PM9/5/20
to
Had exactly you problem, found this link which worked without any fiddling, just add the textArea to a scrollPane, then your scrollPane to your splitPane - https://stackoverflow.com/questions/33198303/resizing-jtextarea-while-using-linewrap/33200995#33200995
0 new messages