File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes jtable and jscrollpane question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "jtable and jscrollpane question" Watch "jtable and jscrollpane question" New topic

jtable and jscrollpane question

jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113

I have a JTable in a JScrollPane. My application is designed to have this table take up most of the screen. Sometimes the table has hundreds of rows of data in it and everything works well.

However, sometimes it only has three rows in it and it looks very bad inside of the scrollPane.

I would like my table to be full screen all the time regardless of the actual number of rows of data it has.

Is there an easy way to do this or do I have to add "blank" rows to my table whenever I detect this condition?
Joe Myn

Joined: Oct 19, 2004
Posts: 25
JScrollPane scrollPane = new JScrollPane(yourTable);
yourTable.setPreferredScrollableViewportSize(new Dimension(int,int ));
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Thanks for the reply but it doesn't seem to have the affect I was looking for.

Take a look at this code:

This frame should fill the screen, but the table will only have six rows in it.

Is it possible to fill the rest of the table with some "blank" rows somehow?
Mikie Williams

Joined: Jan 11, 2005
Posts: 7
try commenting out
table.setPreferredScrollableViewportSize(new Dimension(900, 900));

Then add setExtendedState(JFrame.MAXIMIZED_BOTH) right under
super("Table example, Wines from Bordeaux").

See if that helps. One note. I believe you have to be using a jdk of 1.4.0 or higher though.
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
No, that seemed to be a step back.

It didn't solve the problem and I had to stretch the frame when my application initially loaded.

I guess I'm just going to have to fake this in the program...I just figured that there would be an easy way around this.
Mikie Williams

Joined: Jan 11, 2005
Posts: 7
so your program looks like this and it didn't work. Well I am sorry. It maximizes the window when I run it.

public class MyClass extends JFrame {
public MyClass() {
super("Table example, Wines from Bordeaux");
Object[][] tabledata = {
{ "Chateau Meyney, St. Estephe", new Integer(1994), "$18.75" },
{ "Chateau Montrose, St. Estephe", new Integer(1975), "$54.25" },
{ "Chateau Gloria, St. Julien", new Integer(1993), "$22.99" },
{ "Chateau Beychevelle, St. Julien", new Integer(1970), "$61.63" },
{ "Chateau La Tour de Mons, Margeaux", new Integer(1975), "$57.03" },
{ "Chateau Brane-Cantenac, Margeaux", new Integer(1978),
"$49.92" }, };
String columnheaders[] = { "Wine", "Vintage", "Price" };
JTable table = new JTable(tabledata, columnheaders);
//table.setPreferredScrollableViewportSize(new Dimension(900, 900));
JScrollPane scrollPane = new JScrollPane(table);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {

public static void main(String[] args) {
MyClass mc = new MyClass();;
I agree. Here's the link:
subject: jtable and jscrollpane question
It's not a secret anymore!