Results 1 to 8 of 8
  1. #1
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    From How to Use Scroll Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    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)
    I recommend posting an SSCCE which demos what is different than the explanation above.

  3. #3
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    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.
    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 bar
    Last edited by kreyszig; 02-04-2011 at 07:40 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    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!

  5. #5
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    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?
    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...

  6. #6
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Anyone has an idea about this? Should I submit a problem report?

  7. #7
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    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 there
    Last edited by kreyszig; 02-08-2011 at 03:00 PM.

  8. #8
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

Similar Threads

  1. Strange JVM behaviour
    By pjpr in forum Advanced Java
    Replies: 13
    Last Post: 01-03-2011, 07:39 PM
  2. Weird Behaviour of String
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 08-08-2010, 11:08 PM
  3. BoxLayout Behaviour
    By PetalumaBoy in forum AWT / Swing
    Replies: 4
    Last Post: 06-10-2009, 01:27 PM
  4. Strange behaviour in serialization
    By Wolverine in forum Networking
    Replies: 0
    Last Post: 05-23-2009, 12:03 PM
  5. AffinedTransform strange behaviour
    By Echilon in forum AWT / Swing
    Replies: 3
    Last Post: 12-11-2008, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •