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

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

How to check if a value exists in jTable which I am trying to add?

javaswingjtable

提问by xoxn-- 1'w3k4n

How to check in jTablethe values I am trying to add from jFieldexists 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

JTabledoes not provide this functionality.

JTable不提供此功能。

JTableis 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 TableModelthat 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 Setthis is built in.

JTable只是表模型的可视化。您的数据模型应该具有您需要的业务功能,例如告诉您条目是否存在。您将数据模型包装在TableModel可以由JTable. 然后您只需操作您的数据模型。这是一个contains(或类似的)方法应该存在的地方。如果您的数据模型Set是内置的。

For convenience the data model and the TableModelcan be the same object, sometimes it is appropriate. But just using a Vectoris not a good idea. Sadly JTablehas some convenience methods that allow this for static data.

为方便起见,数据模型和TableModel可以是同一个对象,有时是合适的。但是仅仅使用 aVector并不是一个好主意。可悲的是,JTable有一些方便的方法允许静态数据。

Just build your own TableModelon top of AbstractTableModel. See the JTable tutorial.

只需TableModelAbstractTableModel. 请参阅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;
}