Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find column # by column name or header - JTable

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?

like image 802
Syed Muhammad Mubashir Avatar asked Nov 20 '12 03:11

Syed Muhammad Mubashir


People also ask

How can I see all column names in SQL?

USE db_name; DESCRIBE table_name; it'll give you column names with the type.

How do I find a column in SQL?

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.

How do I find a specific column in a SQL Server database?

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.

How do I find column names in a table?

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.


2 Answers

You should try using this:

int qty = jtable.getValueAt( rowNo, jtable.getColumn("columnName").getModelIndex() );
like image 62
Ankur Avatar answered Oct 10 '22 08:10

Ankur


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();
    }
    ...
}
like image 26
trashgod Avatar answered Oct 10 '22 07:10

trashgod