Results 1 to 7 of 7
  1. #1
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default wrong size of JScrollPane

    Hello, the pieces of my code:
    - my frame is set to:
    Java Code:
    frame.setMinimumSize(new Dimension(640, 480));
    frame.setPreferredSize(new Dimension(640, 480));
    frame.setResizable(false);
    frame.setLocation(100, 100);
    - within frame is panel with padding:
    Java Code:
    JPanel panel = new JPanel(new BorderLayout());
    
    JPanel topPadding = new JPanel();
    topPadding.setPreferredSize(new Dimension(0, 15));
    panel.add(topPadding, BorderLayout.PAGE_START);
    
    JPanel bottomPadding = new JPanel();
    bottomPadding.setPreferredSize(new Dimension(0, 30));
    panel.add(bottomPadding, BorderLayout.PAGE_END);
    
    JPanel leftPadding = new JPanel();
    leftPadding.setPreferredSize(new Dimension(50, 0));
    panel.add(leftPadding, BorderLayout.LINE_START);
    
    JPanel rightPadding = new JPanel();
    rightPadding.setPreferredSize(new Dimension(50, 0));
    panel.add(rightPadding, BorderLayout.LINE_END);
    - the last area is missing - BorderLayout.CENTER and this is my problem:
    Java Code:
    JTable table = new JTable(10, 4);
    table.setFillsViewportHeight(true);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    
    JScrollPane area = new JScrollPane(table);
    area.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
    
    panel.add(area, BorderLayout.CENTER);
    - the rest of my code is fully functional and tested, but the problem is... if I put this piece of code instead of previous:
    Java Code:
    JScrollPane area = new JScrollPane();    // this piece instead of JScrollPane area = new JScrollPane(table);
    everything is correct... frame will stay 640x480... when I use previous version (with table), frame will change its size to 650xMUCH_MORE_THAN_480... I've tried almost everything I've found and I don't know what the solution is... I tried print dimensions of table (viewport of scroll pane):
    Java Code:
    System.out.println(area.getViewport().getViewSize());
    and it prints
    Java Code:
    java.awt.Dimension[width=530,height=160]
    so I think, something is wrong... I did some error :confused: ... any advice? Thnx

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Do you call pack()?

    If you're still having problems, you really should create a small compilable program that demonstrates your problem, an SSCCE, that we can run on our own machines. Otherwise it is pretty hard to debug layout problems with only snips and pieces of code.

  3. #3
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Just curious, why does the code not call frame.setMaximumSize()?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I copied your code into a test app to see what it would do.
    I'm unable to replicate the trouble you are describing.
    Maybe you can modify this to illustrate the problem.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.EtchedBorder;
    
    public class TableSize {
        private JPanel getContentPane() {
            JPanel panel = new JPanel(new BorderLayout());
            panel.setOpaque(true);
    
            JPanel topPadding = new JPanel();
            topPadding.setPreferredSize(new Dimension(0, 15));
            panel.add(topPadding, BorderLayout.PAGE_START);
    
            JPanel bottomPadding = new JPanel();
            bottomPadding.setPreferredSize(new Dimension(0, 30));
            panel.add(bottomPadding, BorderLayout.PAGE_END);
    
            JPanel leftPadding = new JPanel();
            leftPadding.setPreferredSize(new Dimension(50, 0));
            panel.add(leftPadding, BorderLayout.LINE_START);
    
            JPanel rightPadding = new JPanel();
            rightPadding.setPreferredSize(new Dimension(50, 0));
            panel.add(rightPadding, BorderLayout.LINE_END);
            return panel;
        }
    
        private JScrollPane getContent() {
            JTable table = new JTable(10, 4);
             table.setFillsViewportHeight(true);
             table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    
             // Sometimes this comes in handy for sizing.
             //Dimension d = table.getPreferredSize();
             //table.setPreferredScrollableViewportSize(d);
    
             JScrollPane area = new JScrollPane(table);
             area.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
             return area;                  // these both look the
             // try an empty scrolllPane   // same in j2se 1.6 with
    //         return new JScrollPane();   // frame size: [640, 480]
        }
    
        public static void main(String[] args) {
            TableSize test = new TableSize();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setMinimumSize(new Dimension(640, 480));
            f.setPreferredSize(new Dimension(640, 480));
            f.setResizable(false);
            f.setContentPane(test.getContentPane());
            f.add(test.getContent(), BorderLayout.CENTER);
            f.pack();
            f.setLocation(100, 100);
            f.setVisible(true);
            Dimension d = f.getSize();
            System.out.printf("frame size: [%d, %d]%n", d.width, d.height);
        }
    }

  5. #5
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default

    Thank you for advices and I'm sorry for my pieces of code only. I'll write whole compilable programs for next time.
    Now to reply: I've tried make compilable program for you, I've run it and I haven't seen any problems. Everything's looked correct. My project is rather big, so I've tried read the source code by hardwired and I've noticed that the pack() method is in the end, after properties of frame. So I've tried it too, because I had pack() method before properties, and it seems to be almost correct. However, a little difference remains. The window is still not 640x480, but 650x490. It seems to me, that it is some special behaviour of Java. Is it truth, or is the mistake somewhere else? Thank you
    Last edited by BigBear; 01-25-2010 at 04:43 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by BigBear View Post
    ....A little difference remains. The window is still not 640x480, but 650x490. It seems to me, that it is some special behaviour of Java. Is it truth, or is the mistake somewhere else? Thank you
    All I can say is that sizing works for me. If it doesn't work for you, then there is something in your code that doesn't allow it to work well. What that is -- I have no idea.

  7. #7
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 02:37 AM
  2. Changing JTableModel does not change JScrollpane size
    By stelzergil in forum New To Java
    Replies: 3
    Last Post: 10-19-2009, 03:14 AM
  3. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 05:37 PM
  4. jscrollpane
    By kaemonsaionji in forum New To Java
    Replies: 3
    Last Post: 02-25-2009, 09:39 AM
  5. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:58 PM

Posting Permissions

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