Dynamic Creation of JTable

I have a JTable that has columns selected from a options dialog. The problem is that the user can move a selected column up or down, thereby changing its order in the JTable.

Since I am just hiding the columns that the user did not select, will the

getValueAt(int nRow, int nCol) method still work? I am not sure if the nCol value will get messed up when the user changes the sequence of the columns.

TIA.

[432 byte] By [balachandrana] at [2007-10-2 23:25:04]
# 1
> I am not sure if the nCol value will get messed up when the user changes the sequence of the columns.Well, then try it and see what happens. It it doesn't work then you have a real question to ask.
camickra at 2007-7-14 16:03:59 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hiding the columns does not change the index. However the user can change the sequence of the columns in the table. To change the sequence of a jtable during runtime requires moveColumn(vSrcColIndex, vDstColIndex); method.

The sequence of the columns is 0,1,2 in the beginning. After the user makes the selection it is in random order 1,0,2 or something else.

So the question is:

What should be index values for the moveColumn method? or How to associate the random order with the index of moveColumn method?

balachandrana at 2007-7-14 16:03:59 > top of Java-index,Desktop,Core GUI APIs...
# 3
Again the answer it try it.Create a simple table with 4 or 5 columns.Add a button that will move column 3 to position 0.Click the button a few times and see which column is moved. Once you figure out the pattern you have your answer.
camickra at 2007-7-14 16:03:59 > top of Java-index,Desktop,Core GUI APIs...
# 4

Hi Balu,

Any how u have required all the columns of a table in ur scenario.. And also u cannot delete a column from the table.

For that just hide that particular column by using setpreffered width as Zero...

Eg: If u want to hide a cloumn5 ..then write as follows

column5.setPreferredWidth(0);

column5.setMaxWidth(0);

and keep continue to give size to other columns which u requied in ur table..

U have to write a getValue() for all the columns of the Table event though u may not require some of the columns.......

Try on this .....

Regards,

Vk

vasmhi_1981a at 2007-7-14 16:03:59 > top of Java-index,Desktop,Core GUI APIs...
# 5
And also u cannot delete a column from the table.You can write a wrapper TableModel which allows columns to be turned on and off, it's pretty straightforward. I would suggest that's a rather better solution than just setting widths to 0.
itchyscratchya at 2007-7-14 16:03:59 > top of Java-index,Desktop,Core GUI APIs...
# 6
Hi,But the problem is U have to write a unique getValueat() method for urtable ...Even u dynamically displays the tables definetly u face problem when u want to populate the data into the table columns... Just Try...
vasmhi_1981a at 2007-7-14 16:03:59 > top of Java-index,Desktop,Core GUI APIs...
# 7

But the problem is U have to write a unique getValueat() method for ur table

How is this a problem?

Even u dynamically displays the tables definetly u face problem when u want to populate the data into the table columns

No you don't. You have two models. Let's say you use a normal DefaultTableModel (it can be any TableModel) object to actually store your data. Then you have your, say, "ColumnFilteringTableModel" object which references the underlying model and delegates its get/set/editable etc methods to it after modifying the column index accordingly.

You feed your ColumnFilteringTableModel object to the JTable.

You manipulate the data in your DefaultTableModel object.

It works fine, I've already written code which does eactly that :o)

itchyscratchya at 2007-7-14 16:03:59 > top of Java-index,Desktop,Core GUI APIs...
# 8
table.getValueAt() just grabs the value from the viewbelow will use the TableModel which remains unchanged when you modify any of the view aspects of a table.table.getTableModel().getValueAt(.......); data and view are always sererate in swing
DataVirtuea at 2007-7-14 16:03:59 > top of Java-index,Desktop,Core GUI APIs...
# 9

Camickr, it's not that simple to find the pattern. Here is why:

Let's say the initial sequence is [a,b,c,d,e,f] when the user modifies this, it could become [f,a,d,c,b,e]. Your logic will not work in this case.

Tip from Java Almanac :

Converting a Column Index Between the View and Model in a JTable Component

A column in a JTable component has two types of indices, a visible index and a model index. The visible index of a column is its visible location on the screen. The model index of a column is its permanent position in a TableModel, which contains the actual data. This example demonstrates how to convert a column index between the two forms.

Even if I convert the view index to model index I still need to know whether to move up or down and by how many units. Any ideas on this?

null

balachandrana at 2007-7-14 16:03:59 > top of Java-index,Desktop,Core GUI APIs...
# 10
hello balachandran , May u mail me a copy of Java Desktop Live?My mail address is:chenyowa@hotmail.com . Thank u very much.
yowachena at 2007-7-14 16:03:59 > top of Java-index,Desktop,Core GUI APIs...