Results 1 to 7 of 7
Thread: wrong size of JScrollPane
- 01-25-2010, 12:33 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 37
- Rep Power
- 0
wrong size of JScrollPane
Hello, the pieces of my code:
- my frame is set to:- within frame is panel with padding:Java Code:frame.setMinimumSize(new Dimension(640, 480)); frame.setPreferredSize(new Dimension(640, 480)); frame.setResizable(false); frame.setLocation(100, 100);
- the last area is missing - BorderLayout.CENTER and this is my problem: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 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: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);
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:JScrollPane area = new JScrollPane(); // this piece instead of JScrollPane area = new JScrollPane(table);
and it printsJava Code:System.out.println(area.getViewport().getViewSize());
so I think, something is wrong... I did some error :confused: ... any advice? ThnxJava Code:java.awt.Dimension[width=530,height=160]
-
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.
- 01-25-2010, 03:32 AM #3
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
Just curious, why does the code not call frame.setMaximumSize()?
- 01-25-2010, 07:45 AM #4
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); } }
- 01-25-2010, 03:40 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 37
- Rep Power
- 0
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 youLast edited by BigBear; 01-25-2010 at 03:43 PM.
-
- 01-25-2010, 03:48 PM #7
Member
- Join Date
- Jan 2010
- Posts
- 37
- Rep Power
- 0
Similar Threads
-
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 2Last Post: 11-22-2009, 03:48 PM -
Changing JTableModel does not change JScrollpane size
By stelzergil in forum New To JavaReplies: 3Last Post: 10-19-2009, 02:14 AM -
Setting frame size to the size of an image
By Yoruichi in forum AWT / SwingReplies: 5Last Post: 04-22-2009, 04:37 PM -
jscrollpane
By kaemonsaionji in forum New To JavaReplies: 3Last Post: 02-25-2009, 08:39 AM -
help with JScrollPane
By tommy in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 07:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks