This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

JScrollPane, JTable

kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
Hi,
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 ?
Thanks,
Kate
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.
Kate
Javaoops
Ranch Hand

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


cs sakthi
Greenhorn

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.
table.setAutoResizeMode(0);
column = table.getColumnModel().getColumn(columnIndex);
column.setMinWidth(80);
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.
-sakthi.
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 !
Kate
 
GeeCON Prague 2014
 
subject: JScrollPane, JTable