Alexander Leithner Alexander Leithner - 3 years ago 97
Java Question

Expanding a node of a JTree minimizes GridBagLayout

I have created a simple Swing application which currently consists of a JToolBar, a JTree and a RSyntaxTextArea, all inside a GridBagLayout.

The JTree has currently only one top node and that has only one child node.
Complete UI with JToolBar, JTree and RSyntaxTextArea
Complete UI with JToolBar, JTree and RSyntaxTextArea

When expanding the top node of the JTree, the whole GridBagLayout kind of "minimizes":
The UI after expanding the top node of the JTree.

I've googled this phenominum, but since there's no error message or something else in the console, I'm kind of helpless right now.

I'm using the following code to create the UI:

RSyntaxTextArea textArea = new RSyntaxTextArea(50, 150);
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
textArea.setCodeFoldingEnabled(true);
RTextScrollPane sp = new RTextScrollPane(textArea);

cp.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;

c.gridx = 0;
c.gridy = 0;
cp.add(createToolbar(), c);

c.gridx = 0;
c.gridy = 1;
c.ipadx = 90;
c.fill = GridBagConstraints.BOTH;
cp.add(createTree(), c);

c.gridx = 1;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
cp.add(sp, c);

...

private JToolBar createToolbar() {
JToolBar tb = new JToolBar("Toolbar", JToolBar.HORIZONTAL);

JButton ob = new JButton(new ImageIcon("..."));

tb.add(ob);

tb.setFloatable(false);
tb.setRollover(true);

return tb;
}

...

private JTree createTree() {
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Projects");
JTree tree = new JTree(top);

DefaultMutableTreeNode test = new DefaultMutableTreeNode("I'm a test!");
top.add(test);

return tree;
}


Update: A minimal code example to compile on your system for testing purposes:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Tester extends JFrame {

public Tester () {
initializeComponent();
}

private void initializeComponent() {
JPanel cp = new JPanel(new BorderLayout());
JTextArea textArea = new JTextArea(50, 150);
JScrollPane sp = new JScrollPane(textArea);
cp.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
cp.add(createToolbar(), c);
c.gridx = 0;
c.gridy = 1;
c.ipadx = 90;
c.fill = GridBagConstraints.BOTH;
cp.add(createTree(), c);
c.gridx = 1;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
cp.add(sp, c);
setContentPane(cp);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}

private JTree createTree() {
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Projects");
JTree tree = new JTree(top);
DefaultMutableTreeNode test = new DefaultMutableTreeNode("I'm a test!");
top.add(test);
return tree;
}

private JToolBar createToolbar() {
JToolBar tb = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
JButton ob = new JButton("Button");
tb.add(ob);
tb.setFloatable(false);
tb.setRollover(true);
return tb;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Tester().setVisible(true);
}
});
}

}

Answer Source

When expanding the top node of the JTree, the whole GridBagLayout kind of "minimizes":

The GridBagLayout will shrink to the minimum size of a component when there is not enough space to display the entire component.

Swing application which currently consists of a JToolBar, a JTree and a RSyntaxTextArea, all inside a GridBagLayout.

I would just use the default BorderLayout of the frame:

add(toolbar, BorderLayout.PAGE_START);
add(treeScrollPane, BorderLayout.CENTER);
add(textAreaScrollPane, BorderLayout.PAGE_END);

Note how I added the JTree to a JScrollPane. Now scrollbars will appear for the tree when needed.

If you really want to use the GridBagLayout then read the section from the Swing tutorial on How to Use GridBagLayout for an explanation of how to use the various constraints. You may want to start with the "weightx/y" constraints which control which components get space as the frame size is changed. Also, look at the "fill" constraint.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download