Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JComboBox作为JTable列编辑器_Java_Jtable_Jcombobox - Fatal编程技术网

Java JComboBox作为JTable列编辑器

Java JComboBox作为JTable列编辑器,java,jtable,jcombobox,Java,Jtable,Jcombobox,我有一个有四列的JTable。在第二列中,我指定了一个JComboBox作为我列的单元格编辑器。我遇到的问题是,我无法以我想要的方式处理组合框 我想要的是,在用户在组合框中选择一个项目后,程序应该向表格模型中添加一个新行,并将焦点转移到第一列中的这一新行。 请协助,让我们关注上面的粗体要求。如果可能,一个示例代码将是最好的 第2类: public class Model extends AbstractTableModel { protected String[] colNames; p

我有一个有四列的JTable。在第二列中,我指定了一个JComboBox作为我列的单元格编辑器。我遇到的问题是,我无法以我想要的方式处理组合框

我想要的是,在用户在组合框中选择一个项目后,程序应该向表格模型中添加一个新行,并将焦点转移到第一列中的这一新行。

请协助,让我们关注上面的粗体要求。如果可能,一个示例代码将是最好的

第2类:

   public class Model extends AbstractTableModel {

protected String[] colNames;
protected Vector data;

Model(String[] col) {
    this.colNames = col;
    this.data = new Vector();
}

@Override
public int getRowCount() {
    return this.data.size();
}

@Override
public String getColumnName(int col) {
    return this.colNames[col];
}

@Override
public int getColumnCount() {
    return this.colNames.length;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    Product record = (Product) data.get(rowIndex);
    switch (columnIndex) {
        case qi:
            return record.getQuantity();
        case di:
            return record.getDecsription();
        case ui:
            return record.getPrice();
        case ti:
            return record.getTprice();
        default:
            return new Object();
    }

}

@Override
public boolean isCellEditable(int row, int col) {
   return col<=3;

}

@Override
public void setValueAt(Object value, int row, int column) {
    Product record = (Product) data.get(row);
    switch (column) {
        case qi:
            record.setQuantity((Integer) value);
            break;
        case di:
            record.setDecsription((String) value);
            break;
        case ui:
            record.setPrice((Double) value);
            break;
        case ti:
            record.setTprice((Double) value);
            break;
        default:
            System.out.println("invalid index");
    }
    fireTableCellUpdated(row, column);//this is for updating the cell
}

@Override
public Class getColumnClass(int column) {// returns a class representing the datatype of the data stored in that column
    switch (column) {
        case qi:
            return Integer.class;
        case di:
            return String.class;
        case ui:
        case ti:
            return Double.class;
        default:
            return Object.class;
    }
}

public void addEmptyRow() {
    data.add(new Product());
    fireTableRowsInserted(
            data.size() - 1,
            data.size() - 1);

}

public boolean hasEmptyRow() {
    if (data.isEmpty()) {
        return false;
    }
    Product productRecord = (Product) data.get(data.size() - 1);
    if (productRecord.getDecsription().trim().equals("")
            && productRecord.getPrice() == 0.0
            && productRecord.getQuantity() == 0
            && productRecord.getTprice() == 0) {
        return true;
    } else {
        return false;
    }}

现在我知道问题出在哪里了。TableColumnRenderer应该将您想要的效果绑定到您创建的组合框上的侦听器

不应该在获得渲染器(或者更确切地说是交互渲染器)时调用它

这部分是罪魁祸首。实际上,您要做的是将其移动到JComboBox上的侦听器中:

public MyComboBoxRenderer(String[] items) {
    super(items);
    // Java 8. Should be easy to convert to anonymous class
    setActionListener(e -> { 
        Table.this.model.addEmptyRow(); 
        this.highlightLastRow(); 
    });
}

缺少产品类。您的代码无法运行。您还有几个原始类型变量。@GilbertLeBlanc根据我的建议,产品类被删除了。不可运行的。。。为什么?添加一个模拟产品。此外,我怀疑rawtype是否与该问题有关described@Vogel612问题是,如果您选择了一个项目,程序将继续添加/追加新行,就像它在某种循环中一样。我希望每次用户在组合框中选择一个项目时,它都会添加一行。@Volgel1612我已经按照您上面所说的做了,但它正在连续添加/追加该行-不间断-与以前一样。
    if (column == interactiveColumn && hasFocus) {
        if ((Table.this.model.getRowCount() - 1) == row
                && !Table.this.model.hasEmptyRow()) {
            Table.this.model.addEmptyRow();//this is where we append a new row
        }

        highlightLastRow(row);//making it get focus and highlighted
    }
public MyComboBoxRenderer(String[] items) {
    super(items);
    // Java 8. Should be easy to convert to anonymous class
    setActionListener(e -> { 
        Table.this.model.addEmptyRow(); 
        this.highlightLastRow(); 
    });
}