Results 1 to 8 of 8
- 02-04-2011, 03:37 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
Weird Behaviour of JScrollPane.getPreferredSize() when using SCROLLBAR_AS_NEEDED
Hi,
I am trying to use the height returned by JScrollPane.getPreferredSize() such that the custom layout manager I wrote for its parent could set the size of the scroll pane properly. However the behaviour of that function is pretty weird when using VERTICAL_SCROLLBAR_AS_NEEDED and HORIZONTAL_SCROLLBAR_AS_NEEDED, to the point I am almost wondering if it is a bug in jdk 1.6.0.3p4. Here is what it does:
-When there is no horizontal scroll bar, the height of a horizontal scroll bar IS included in getPreferredSize().height
-When there is a horizontal scroll bar, the height of the scroll bar is NOT included in getPreferredSize().height
So to me it looks like getPreferredSize() is doing the opposite it should be doing. Am I missing something?
Thanks!Last edited by kreyszig; 02-04-2011 at 03:43 PM.
- 02-04-2011, 05:15 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
From How to Use Scroll Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
I recommend posting an SSCCE which demos what is different than the explanation above.Unless you explicitly set a scroll pane's preferred size, the scroll pane computes it based on the preferred size of its nine components (the viewport, and, if present, the two scroll bars, the row and column headers, and the four corners)
- 02-04-2011, 07:37 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
Here is the simplest code that reproduces the problem:
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
public class Test
{
private static final JPanel framePanel=new JPanel(null);
public static void main(String args[]) {
JFrame frame=new JFrame();
frame.setPreferredSize(new Dimension(860, 675));
JPanel innerPanel=new JPanel(null);
innerPanel.setPreferredSize(new Dimension(300,300));
innerPanel.setMinimumSize(new Dimension(300,300));
innerPanel.setMaximumSize(new Dimension(300,300));
JScrollPane sPane=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEED ED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sPane.setSize(150,150);
sPane.setViewportView(innerPanel);
framePanel.add(sPane);
frame.add(framePanel);
frame.pack();
frame.setVisible(true);
Dimension size=sPane.getPreferredSize();
sPane.setSize(sPane.getWidth(),size.height);
size=sPane.getPreferredSize();
System.out.println("width: "+size.width+" height: "+size.height);
// sPane.setSize(sPane.getWidth(),size.height+(Intege r)UIManager.get("ScrollBar.width"));
// size=sPane.getPreferredSize();
// System.out.println("width: "+size.width+" height: "+size.height);
}
}
The code prints "width: 303 height: 303", so the returned preferred size does not include the width/height of the scroll bars as it is supposed to. Enabling the lines that are commented out allows to get rid of the vertical scroll bar only because I add the height of the horizontal scroll bar. If one tries using sPane.setSize(sPane.getWidth(),size.height), the vertical bar won't disappear because getPreferredSize did not include the height of the horizontal scroll barLast edited by kreyszig; 02-04-2011 at 07:40 PM.
- 02-04-2011, 08:04 PM #4
I'm not sure what the problem is. The preferred size seems to be the same in either case- that seems to mean that the viewport gets more height when you don't have a horizontal scroll bar. That makes sense. How is this weird?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-04-2011, 08:25 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
The scroll pane is supposed to compute its preferred size using the preferred size of the inner panel and the preferred size of the scroll bars if existent, but it clearly does not take into account the height of the existing horizontal scroll bar here, because the preferred scroll pane height is not equal to the preferred inner panel height + the horizontal scroll bar height + applicable insets...
- 02-07-2011, 02:28 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
Anyone has an idea about this? Should I submit a problem report?
- 02-07-2011, 09:42 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
ok, I just tested with OpenJDK and it does not behave the same way. So this is definitely a bug in jdk 1.6.0.3p4
Edit: I checked the latest version of JDK 1.6 (revision 23) and the bug is still thereLast edited by kreyszig; 02-08-2011 at 03:00 PM.
- 02-08-2011, 02:23 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
A bug report has been submitted: http://bugs.sun.com/bugdatabase/view...bug_id=7017757
Last edited by kreyszig; 02-08-2011 at 02:48 PM.
Similar Threads
-
Strange JVM behaviour
By pjpr in forum Advanced JavaReplies: 13Last Post: 01-03-2011, 07:39 PM -
Weird Behaviour of String
By diskhub in forum New To JavaReplies: 6Last Post: 08-08-2010, 11:08 PM -
BoxLayout Behaviour
By PetalumaBoy in forum AWT / SwingReplies: 4Last Post: 06-10-2009, 01:27 PM -
Strange behaviour in serialization
By Wolverine in forum NetworkingReplies: 0Last Post: 05-23-2009, 12:03 PM -
AffinedTransform strange behaviour
By Echilon in forum AWT / SwingReplies: 3Last Post: 12-11-2008, 09:58 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks