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?
USE db_name; DESCRIBE table_name; it'll give you column names with the type.
Query: SELECT * FROM INFORMATION_SCHEMA. COLUMNS WHERE COLUMN_NAME LIKE 'ENGLISH%' ORDER BY TABLE_NAME; Note – All columns returned to have a prefix of 'ENGLISH' in their names.
USE YourDatabseName GO SELECT t.name AS table_name, SCHEMA_NAME(schema_id) AS schema_name, c.name AS column_name FROM sys. tables AS t INNER JOIN sys. columns c ON t. OBJECT_ID = c.
We can verify the data in the table using the SELECT query as below. We will be using sys. columns to get the column names in a table. It is a system table and used for maintaining column information.
You should try using this:
int qty = jtable.getValueAt( rowNo, jtable.getColumn("columnName").getModelIndex() );
You should probably be asking the TableModel
, rather than the JTable
, which may have its columns rearranged. One approach would be to let your TableModel
implement a suitable interface, for example,
public interface Quantifiable {
public int getQuantity(int row);
}
Addendum: Please tell how to implement this interface.
Much depends on the relationship among your existing TableModel
classes. Lets say the all have a numeric quantity in some column. If quantityCol
is the model index a column having the type Number
, you could 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();
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With