Find column # by column name or header - JTable

Syed Muhammad Mubashir picture Syed Muhammad Mubashir · Nov 20, 2012 · Viewed 15.1k times · Source

I want to implement a general validation class for my jtables in different forms to check the qty column , as the qty column No in different tables of different forms is different. For this i want to get the column value by column Name similarly in C# or VB.

My requirement is as follows.

 int qty=jtable.getValueAt(rowNo,"columnName");

Now i am using

 int qty=jtable.getValueAt(rowNo,colNo);

Is there any way to find column # by column Name or Header Of JTable?

Answer

Ankur picture Ankur · Dec 4, 2013

You should try using this:

int qty = jtable.getValueAt( rowNo, jtable.getColumn("columnName").getModelIndex() );