Harish
Harish

Reputation: 3483

single column of JTable into a scroll pane

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

Upvotes: 2

Views: 1501

Answers (3)

camickr
camickr

Reputation: 324197

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.

Upvotes: 1

Kevin Day
Kevin Day

Reputation: 16413

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

Upvotes: 2

Chuk Lee
Chuk Lee

Reputation: 3608

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.

Upvotes: 1

Related Questions