[英]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. 我想以不同的形式为我的jtables实现一个通用的验证类来检查qty列,因为不同形式的不同表中的qty列No是不同的。 For this i want to get the column value by column Name similarly in C# or VB.
为此,我希望在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标题查找列#?
你应该尝试使用这个:
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. 您可能应该询问
TableModel
,而不是JTable
,它可能会重新排列其列。 One approach would be to let your TableModel
implement a suitable interface, for example, 一种方法是让你的
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 TableModel
classes. 很大程度上取决于您现有的
TableModel
类之间的关系。 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: 如果
quantityCol
是具有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();
}
...
}
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);
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
,否则你可能会相应地对待它。
嗨这是你的问题的简单答案:
int qty=jtable.getValueAt(rowNo,jtable.convertColumnIndexToView(jtable.getColumn("columnName").getModelIndex()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.