-
Bug
-
Resolution: Not an Issue
-
P3
-
None
-
8u45
A DESCRIPTION OF THE PROBLEM :
When "JScrollPane" containing a "JTree" in a "JPanel" with a "GridBagLayout" is used, "JScrollPane" behaves, as if it had a fixed size. The user considers this fact as a bug.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
The test case "TreeTest.java", whose source code is listed below, demonstrates the issue. Note that the content panel has a vertical scroll bar even though the "JTree" is quite small. Also note that the "spForTree.getPreferredSize()" is returning a set size even though the test case code has not set it.
The user's question: is there any way to avoid the scrollbar when the tree is small? The user tried to apply following measures:
a) Explicit call "spForTree.setPreferredSize(null)" to clear the preferred size. This had no effect. Subsequent call to "getPreferredSize()" still returned Dimension(123,363).
b) Explicit call "spForTree.setPreferredSize(new Dimension(100,100))" to set a smaller size. This worked, but the user did not want a fixed size. The user needs the preferred size to be removed so that the scroll pane just uses the available space.
c) Extending "JScrollPane" and overriding the "getPreferredSize()" method to always return null. This resulted in a "NullPointerException" at "java.awt.GridBagLayout.GetLayoutInfo(GridBagLayout.java:1118)".
---------- BEGIN SOURCE ----------
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
public class TreeTest {
public TreeTest() {
super();
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(500, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel m_contentPanel = new JPanel();
JScrollPane spForContent = new JScrollPane(m_contentPanel);
m_contentPanel.setLayout(new GridBagLayout());
frame.add(spForContent);
JTree tree = new JTree();
JScrollPane spForTree = new JScrollPane(tree);
m_contentPanel.add(spForTree, new GridBagConstraints(
0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST,
GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TreeTest test = new TreeTest();
}
});
}
}
---------- END SOURCE ----------
When "JScrollPane" containing a "JTree" in a "JPanel" with a "GridBagLayout" is used, "JScrollPane" behaves, as if it had a fixed size. The user considers this fact as a bug.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
The test case "TreeTest.java", whose source code is listed below, demonstrates the issue. Note that the content panel has a vertical scroll bar even though the "JTree" is quite small. Also note that the "spForTree.getPreferredSize()" is returning a set size even though the test case code has not set it.
The user's question: is there any way to avoid the scrollbar when the tree is small? The user tried to apply following measures:
a) Explicit call "spForTree.setPreferredSize(null)" to clear the preferred size. This had no effect. Subsequent call to "getPreferredSize()" still returned Dimension(123,363).
b) Explicit call "spForTree.setPreferredSize(new Dimension(100,100))" to set a smaller size. This worked, but the user did not want a fixed size. The user needs the preferred size to be removed so that the scroll pane just uses the available space.
c) Extending "JScrollPane" and overriding the "getPreferredSize()" method to always return null. This resulted in a "NullPointerException" at "java.awt.GridBagLayout.GetLayoutInfo(GridBagLayout.java:1118)".
---------- BEGIN SOURCE ----------
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
public class TreeTest {
public TreeTest() {
super();
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(500, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel m_contentPanel = new JPanel();
JScrollPane spForContent = new JScrollPane(m_contentPanel);
m_contentPanel.setLayout(new GridBagLayout());
frame.add(spForContent);
JTree tree = new JTree();
JScrollPane spForTree = new JScrollPane(tree);
m_contentPanel.add(spForTree, new GridBagConstraints(
0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST,
GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TreeTest test = new TreeTest();
}
});
}
}
---------- END SOURCE ----------