java 按列名或标题查找列# - JTable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13466240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:52:09  来源:igfitidea点击:

Find column # by column name or header - JTable

javaswingjtabletablecolumn

提问by Syed Muhammad Mubashir

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.

我想为我的不同形式的jtables实现一个通用的验证类来检查qty列,因为不同形式的不同表中的qty列No是不同的。为此,我想在 C# 或 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?

有没有办法按列名或 JTable 的标题查找列 #?

采纳答案by Syed Muhammad Mubashir

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

使用Bob Camick 的表格单元格编辑器的通用表格单元格侦听器的完整代码

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

回答by Ankur

You should try using this:

你应该尝试使用这个:

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

回答by trashgod

You should probably be asking the TableModel, rather than the JTable, which may have its columns rearranged. One approach would be to let your TableModelimplement a suitable interface, for example,

您可能应该询问TableModel,而不是JTable,这可能会重新排列其列。一种方法是让您TableModel实现一个合适的接口,例如,

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

Addendum: Please tell how to implement this interface.

附录:请告诉如何实现这个接口

Much depends on the relationship among your existing TableModelclasses. Lets say the allhave a numeric quantity in somecolumn. If quantityColis the model index a column having the type Number, you could do something like this:

在很大程度上取决于您现有TableModel类之间的关系。比方说,在所有的有一个数,一些列。如果quantityCol模型索引是具有 type 的列,则Number可以执行以下操作:

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

回答by muriloap

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.

当然,如果是你创建了它预期永远不会返回的表头-1,否则你可以相应地对待它。

回答by Mimmo Cuomo

Hi this is a simple answer to Your the question:

嗨,这是对您的问题的简单回答:

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