java 如何检查我要添加的 jTable 中是否存在值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15639611/
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
How to check if a value exists in jTable which I am trying to add?
提问by xoxn-- 1'w3k4n
How to check in jTable
the values I am trying to add from jField
exists or not ? I am using Vector<Object>
in jTable
.
如何检查jTable
我尝试添加的值是否jField
存在?我正在使用Vector<Object>
中jTable
。
回答by Ajusal Sugathan
This should do it.
这应该做。
public boolean existsInTable(JTable table, Object[] entry) {
// Get row and column count
int rowCount = table.getRowCount();
int colCount = table.getColumnCount();
// Get Current Table Entry
String curEntry = "";
for (Object o : entry) {
String e = o.toString();
curEntry = curEntry + " " + e;
}
// Check against all entries
for (int i = 0; i < rowCount; i++) {
String rowEntry = "";
for (int j = 0; j < colCount; j++)
rowEntry = rowEntry + " " + table.getValueAt(i, j).toString();
if (rowEntry.equalsIgnoreCase(curEntry)) {
return true;
}
}
return false;
}
回答by Hauke Ingmar Schmidt
JTable
does not provide this functionality.
JTable
不提供此功能。
JTable
is only a visualization for a table model. Your data model should have the business functions you need, like telling you if an entry exists. You wrap the data model inside a TableModel
that can be displayed by the JTable
. You then only manipulate your data model. This is the place where a contains
(or similar) method should exists. If your data model is a Set
this is built in.
JTable
只是表模型的可视化。您的数据模型应该具有您需要的业务功能,例如告诉您条目是否存在。您将数据模型包装在TableModel
可以由JTable
. 然后您只需操作您的数据模型。这是一个contains
(或类似的)方法应该存在的地方。如果您的数据模型Set
是内置的。
For convenience the data model and the TableModel
can be the same object, sometimes it is appropriate. But just using a Vector
is not a good idea. Sadly JTable
has some convenience methods that allow this for static data.
为方便起见,数据模型和TableModel
可以是同一个对象,有时是合适的。但是仅仅使用 aVector
并不是一个好主意。可悲的是,JTable
有一些方便的方法允许静态数据。
Just build your own TableModel
on top of AbstractTableModel
. See the JTable tutorial.
只需TableModel
在AbstractTableModel
. 请参阅JTable 教程。
回答by Gyro Gearless
Vector<Object>
makes a pretty poor model for a JTable!
Vector<Object>
为 JTable 制作了一个非常糟糕的模型!
You should write your own model class, perhaps derived from javax.swing.table.AbstractTableModel, something like
您应该编写自己的模型类,可能派生自 javax.swing.table.AbstractTableModel,类似于
public class FooModel extends javax.swing.table.AbstractTableModel {
// AbstractTableModel
public int getRowCount() { //TBD }
public int getColumnCount() { //TBD }
public Object getValueAt(int row, int column) { //TBD }
// Insert a value into model only if not exists yet
public void insertData(String something) throws AlreadyExistsException {
// Check if element already in model
// Either throw exception or update model
// Dont forget to call fireTableRowsInserted() !
}
}
}
回答by xoxn-- 1'w3k4n
Have accomplished my question like this!
像这样完成了我的问题!
private boolean isEntry(String name, String size) {
int rowCount = jTable.getRowCount();
String row = null, tname = null, tsize = null, input = name + "|" + size;
for (int i = 0; i < rowCount - 1; i++) {
tname = (String) jTable.getValueAt(i, 0);
tsize = (String) jTable.getValueAt(i, 2);
row = tname + "|" + tsize;
if (input.equalsIgnoreCase(row)) {
return true;
}
}
return false;
}