Find column # by column name or column header - JTable

I want to implement a common validation class for my jtables in different forms to validate the qty column, since the qty column No in different tables of different forms is different. To do this, I want to get the column value from the Name column in the same way in C # or VB.

My requirement is as follows.

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

Now i use

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

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

+4
source share
5 answers

I accomplished my task using the ternary operator in my code

  int colNo = ((tbl.getModel().getColumnName(tcl.getColumn()).equalsIgnoreCase("Qty")) || (tbl.getModel().getColumnName(tcl.getColumn()).equalsIgnoreCase("Weight")) || (tbl.getModel().getColumnName(tcl.getColumn()).equalsIgnoreCase("Wt")) ? tcl.getColumn() : -1); 

Full code of my common table cell listener using Bob Camick Cell Editor) !

 final JTable table = (JTable) jComp.get(a); tbl.getTableHeader().setReorderingAllowed(false); Action actionProd = new AbstractAction() { public void actionPerformed(ActionEvent e) { Utility util = new Utility("GoldNew"); TableCellListener tcl = (TableCellListener) e.getSource(); System.out.println("Row : " + tcl.getRow()); System.out.println("Column: " + tcl.getColumn()); System.out.println("Old : " + tcl.getOldValue()); System.out.println("New : " + tcl.getNewValue()); int colNo = ((table.getModel().getColumnName(tcl.getColumn()).equalsIgnoreCase("Qty")) || (table.getModel().getColumnName(tcl.getColumn()).equalsIgnoreCase("Weight")) || (table.getModel().getColumnName(tcl.getColumn()).equalsIgnoreCase("Wt")) ? tcl.getColumn() : -1); if (tcl.getColumn() == colNo) { int wt = 0; Object qtyO = tcl.getNewValue(); try { qtyO = tcl.getNewValue(); if (qtyO != null) { wt = Integer.parseInt(qtyO.toString()); } if (wt < 0) { table.getModel().setValueAt(tcl.getOldValue(), tcl.getRow(), colNo); } } catch (Exception ex) { util.ShowMessage("Please enter the Numbers only", "Error!"); table.getModel().setValueAt(tcl.getOldValue(), tcl.getRow(), colNo); ex.printStackTrace(); } } } }; TableCellListener tclProd = new TableCellListener(table, actionProd); 
+1
source

You should try:

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

You should TableModel ask for a TableModel , not a JTable , which can have its columns rearranged. One approach is to allow your TableModel implement a suitable interface, e.g.

 public interface Quantifiable { public int getQuantity(int row); } 

Application: Tell us how to implement this interface.

Much depends on the relationship between existing TableModel classes. Suppose everyone has a number in some column. If quantityCol is the model index, a column of type Number , you can do something like this:

 public class QuantifiableTableModel extends AbstractTableModel implements Quantifiable { private int quantityCol; public QuantifiableTableModel(int quantityCol) { this.quantityCol = quantityCol; } @Override public int getQuantity(int row) { Number n = (Number) getValueAt(row, quantityCol); return n.intValue(); } ... } 
+6
source

I ran into the same problem. This was my implementation:

 int nameColNo = getColumnIndex(table, "Name"); String name = (String)table.getValueAt(rowNo, nameColNo); private int getColumnIndex (JTable table, String header) { for (int i=0; i < table.getColumnCount(); i++) { if (table.getColumnName(i).equals(header)) return i; } return -1; } 

Of course, if you created the table headers, they would never return -1 , otherwise you could process it accordingly.

+1
source

Hi, this is a simple answer to your question:

 int qty=jtable.getValueAt(rowNo,jtable.convertColumnIndexToView(jtable.getColumn("columnName").getModelIndex())); 
+1
source

Source: https://habr.com/ru/post/1447126/


All Articles