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 JScrollPane, JTable  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JScrollPane, JTable  " Watch "JScrollPane, JTable  " New topic

JScrollPane, JTable

kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
I have several JTables in JScrollPanes. Most have 6 or so columns of data and look great. One has thirty columns. I have the scrollpanes set to HORIZONTAL_SCROLLPANE_ALWAYS.
The table with lots of columns displays all, very small. I want to set the sizes so that most of them are not in the displayed area, the user should scroll to reach them. So far, all attempts at setting the width of a column have been unsuccessful.
Also, I suspect that the horizontal scroll bar isn't working anyway. THe vertical one is fine - it has a purple slider on it and I can scoll. However, even if I resize the columns manually to really wide, so that some columns would have to disappear out of view, I get no purple slider on the horizontal bar. Instead, the columns simply seem to disappear.
any ideas ?
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
You need the get the TableColumn like this column = table.getColumnModel().getColumn(i). Then you can set the width column.setPreferredWidth(##). You can also set minimum width.
kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
Hi Paul,
thanks for that. setMinWidth does have the desired effect, that I can make the first few columns wide enough for their data. However, the scrollbar steadfastly refuses to scroll, so I'm just losing colmns now.
Ranch Hand

Joined: Jun 21, 2001
Posts: 57
Hi Kate,
Did u try this.
TableColumnModel colModel = table.getColumnModel();

cs sakthi

Joined: May 11, 2001
Posts: 15
Hai Kate,
I guess you should be successfull in setting the preferred column widths in JTable, yet you will not be seeing the scroll pane, since if we scroll some of the columns, the adjacent columns gets adjusted and fits within the viewport area, hence to have the HORIZONTAL SCROLL BAR always, set the minimum width for all the columns and set the setAutoResizeMode(0), you will get the scroll bar, especially for the table with more no. of columns.
column = table.getColumnModel().getColumn(columnIndex);
after setting this, as usual you add the table to the scroll pane, you will get the horizontal sliding successfully.
if possible, let me know your result..
Have a great day.
kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
Hi Sakti, Rajan, Paul,..
thankyou for your help ... my tables look lovely now - that line about setAutoResizeMode(0); gave me back my scroller.
Fantastic !
I agree. Here's the link:
subject: JScrollPane, JTable
jQuery in Action, 3rd edition