Frage

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?

War es hilfreich?

Lösung 4

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);

The full code of my general Table Cell Listener using Bob Camick's Table 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);       

Andere Tipps

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();
    }
    ...
}

Hi this is a simple answer to Your the question:

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

I was facing the same issue. 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 it was you who created the table headers its expected to never return -1, else you might treat it accordingly.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top