tags:

views:

253

answers:

3

Can we add a single column of a JTable inside a scroll pane?

A: 

Not directly. One way of doing it is to create a model for both of them. Say you have a model for JTable

public class MyTableModel implements TableModel, ListModel {

Then you set the model to your JTable and your JList.

Chuk Lee
You'd have to keep any vertical scrolling in sync - you would use addAdjustmentListener to do this. Making the left 'column' width adjustable would be a bit tricky here - you'd have to implement a drag handler that interacts with the JList's width. For what it's worth, it would probably be easier to do this with two JTables, then add a listener to detect changes in the column width.
Kevin Day
+1  A: 

Your question really doesn't make any sense to me so I'm just making a wild guess

A TableModel can be used by many tables. So you can easily create a JTable using a TableModel. Then you can use the TableColumnModel to remove TableColumns from the view. Therefore only a single column will be visible in the scrollpane.

Edit:

To scroll horizontally you use:

table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );

Then you set the TableColumn to your preferred width and scrollbar will appear if required. Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" for more information about setting the column width.

camickr
To make sense I have a very big column which is 255 charecters long and I need to add that column in a scroll pane and all other columns should be added in another scroll pane.
Harish
Still doesn't make sense to me. Whats the point of having one column in a scroll pane and another another scroll pane with the other columns? You can either create two TableModels or as I suggested above share the TableModel and then remove TableColumns as you require.
camickr
Harish - are you asking about horizontal scrolling maybe?
Kevin Day
Yes for a single column horizontally.
Harish
See above edit.
camickr
+1  A: 

If you are asking about a horizontal scroll bar on just a single column, the following technique (viewport tweaking) will probably do the trick:

http://www.java2s.com/Code/Java/Swing-Components/FixedTableColumnExample.htm

Kevin Day