Java 如何仅在JTable中的特定单元格中设置JComboBox

Java 如何仅在JTable中的特定单元格中设置JComboBox,java,swing,jtable,jcombobox,tablecelleditor,Java,Swing,Jtable,Jcombobox,Tablecelleditor,我只想在假设有一个值列表的单元格中添加一个JComboBox。下面是我的代码,但它在列中的所有单元格中添加了组合框。让我知道我的代码中缺少了什么,以便只在所选单元格上设置组合框 public class PropertiesTableModel extends AbstractTableModel{ //this method is called to set the value of each cell @Override public Object getVal

我只想在假设有一个值列表的单元格中添加一个JComboBox。下面是我的代码,但它在列中的所有单元格中添加了组合框。让我知道我的代码中缺少了什么,以便只在所选单元格上设置组合框

     public class PropertiesTableModel extends AbstractTableModel{

 //this method is called to set the value of each cell
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Field field= (Field) fieldList.get(rowIndex);

        if(columnIndex==0){
            String dataType=field.getFieldDef().getDataType();
            return PropertiesPanel.getPpIns().getDataTypeIcon(dataType);
        }

        if(columnIndex==1){
            return field.getFieldDef().getfName();
        }
        else if (columnIndex==2){
             if(field.getFieldDef().getListValue().size()>0){
                 return createValueListCombo(field.getFieldDef().getListValue());
             }

            return field.getDefaultValue();
        }
        else{
            return null;
        }
    }

        public JComboBox createValueListCombo(List<Value> valueList){
        TableColumn valColumn = table.getColumnModel().getColumn(2);
        JComboBox comboBox=new JComboBox();

        for (Value val: valueList) {
            comboBox.addItem(val.getDescription());
        }
        comboBox.setSelectedIndex(0);
        valColumn.setCellEditor(new DefaultCellEditor(comboBox));
        return comboBox;
    }
}
公共类PropertiesTableModel扩展了AbstractTableModel{
//调用此方法以设置每个单元格的值
@凌驾
公共对象getValueAt(int行索引、int列索引){
Field=(Field)fieldList.get(rowIndex);
如果(columnIndex==0){
字符串数据类型=field.getFieldDef().getDataType();
返回属性panel.getPpIns().getDataTypeIcon(数据类型);
}
如果(columnIndex==1){
返回字段.getFieldDef().getfName();
}
else if(columnIndex==2){
如果(field.getFieldDef().getListValue().size()>0){
返回createValueListCombo(field.getFieldDef().getListValue());
}
返回字段。getDefaultValue();
}
否则{
返回null;
}
}
公共JComboBox createValueListCombo(列表valueList){
TableColumn valColumn=table.getColumnModel().getColumn(2);
JComboBox comboBox=新的JComboBox();
对于(值val:valueList){
comboBox.addItem(val.getDescription());
}
comboBox.setSelectedIndex(0);
setCellEditor(新的DefaultCellEditor(组合框));
返回组合框;
}
}

这非常简单,可以用两种方法完成

首先,您的模型应该通知编辑器/表格当前单元格具有值列表

public class PropertiesTableModel extends AbstractTableModel {
     @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        // previous stuff
        if (columnIndex == 2) {
            // return the actually selected value, 
            // not the first value of the list!
            // also the values changed by setValueAt() must be considered.
            return null; // implement it!
        }
    }

    public List<Object> getPossibleValues(int row, int column) {
        // this method should return possible values to select.
        // if cell has no possible values and should be editeb 
        // by a text field this methos should return null
        if (column == 2) {
             Field field= (Field) fieldList.get(rowIndex);
             if (field.getFieldDef().getListValue().size() > 0) {
                 return field.getFieldDef().getListValue();
             }
             return null; // probably something else for non-list values
         }
    }

    public void setValueAt(int row, int column) {
        // you need to store the value chosen by the user
    }
}
2) 您可以创建一个自定义编辑器,该编辑器将所有调用委托给依赖于当前编辑的单元格的两个(或多个)编辑器之一

public class CellEditorMulticaster implements TableCellEditor {
    private DefaultTableCellEditor textEditor;
    private DefaultTableCellEditor currentEditor;

    public CellEditorMulticaster() {
        firstEditor = new DefaultTableCellEditor(new JTextField());
    }

    Component getTableCellEditorComponent(JTable table, Object value,
                                      boolean isSelected,
                                      int row, int column) {
        PropertiesTableModel model = (PropertiesTableModel) table.getModel();
        List<Object> values = model.getPossibleValues(row, column);
        if (values != null) {
            currentEditor = new DefaultCellEditor(new JComboBox(values.toArray()));
        } else {
            currentEditor = textEditor;
        }
        return currentEditor.getTableCellEditorComponent(table, value,
               isSelected, row, column);
    }

    public Object getCellEditorValue() {
        return currentEditor.getCellEditorValue();
    }

    public boolean isCellEditable(EventObject anEvent) {
        JTable table = (JTable) anEvent.getSource;
        int row, col;
        if (anEvent instanceof MouseEvent) {
            MouseEvent evt = (MouseEvent) anEvent;
            row = table.rowAtPoint(evt.getPoint());
            col = table.columnAtPoint(evt.getPoint());
        } else {
            row = table.getSelectedRow();
            col = table.getSelectedColumn();
        }
        PropertiesTableModel model = (PropertiesTableModel) table.getModel();
        List<Object> values = model.getPossibleValues(row, column);
        if (values != null) {
            return true;
        } else {
            return textEditor.isCellEditable(anEvent);
        }
    }

    public boolean shouldSelectCell(EventObject anEvent) {
        return true;
    }

    public boolean stopCellEditing() {
        return currentEditor.stopCellEditing();
    }

    public void  cancelCellEditing() {
        currentEditor.cancelCellEditing();
    }

    // same pattern for another methods (delegate to currentEditor)
}   
公共类CellEditorMulticaster实现TableCellEditor{
私有DefaultTableCellEditor文本编辑器;
私有DefaultTableCellEditor当前编辑器;
公共CellEditorMulticaster(){
firstEditor=新的DefaultTableCellEditor(新的JTextField());
}
组件getTableCellEditorComponent(JTable表、对象值、,
他当选了,,
整数行,整数列){
PropertiesTableModel model=(PropertiesTableModel)table.getModel();
列表值=model.getPossibleValues(行、列);
如果(值!=null){
currentEditor=新的DefaultCellEditor(新的JComboBox(values.toArray());
}否则{
currentEditor=文本编辑器;
}
返回currentEditor.getTableCellEditorComponent(表、值、,
i选定,行,列);
}
公共对象getCellEditorValue(){
返回currentEditor.getCellEditorValue();
}
公共布尔值isCellEditable(EventObject anEvent){
JTable table=(JTable)anEvent.getSource;
int row,col;
if(MouseEvent的事件实例){
MouseEvent evt=(MouseEvent)anEvent;
行=table.rowAtPoint(evt.getPoint());
col=table.columnAtPoint(evt.getPoint());
}否则{
行=表。getSelectedRow();
col=table.getSelectedColumn();
}
PropertiesTableModel model=(PropertiesTableModel)table.getModel();
列表值=model.getPossibleValues(行、列);
如果(值!=null){
返回true;
}否则{
返回textEditor.isCellEditable(anEvent);
}
}
公共布尔值shouldSelectCell(EventObject anEvent){
返回true;
}
公共布尔stopCellEditing(){
返回currentEditor.stopCellEditing();
}
公共作废取消编辑(){
currentEditor.CancellEditing();
}
//其他方法的模式相同(委托给currentEditor)
}   

这非常简单,可以用两种方法完成

首先,您的模型应该通知编辑器/表格当前单元格具有值列表

public class PropertiesTableModel extends AbstractTableModel {
     @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        // previous stuff
        if (columnIndex == 2) {
            // return the actually selected value, 
            // not the first value of the list!
            // also the values changed by setValueAt() must be considered.
            return null; // implement it!
        }
    }

    public List<Object> getPossibleValues(int row, int column) {
        // this method should return possible values to select.
        // if cell has no possible values and should be editeb 
        // by a text field this methos should return null
        if (column == 2) {
             Field field= (Field) fieldList.get(rowIndex);
             if (field.getFieldDef().getListValue().size() > 0) {
                 return field.getFieldDef().getListValue();
             }
             return null; // probably something else for non-list values
         }
    }

    public void setValueAt(int row, int column) {
        // you need to store the value chosen by the user
    }
}
2) 您可以创建一个自定义编辑器,该编辑器将所有调用委托给依赖于当前编辑的单元格的两个(或多个)编辑器之一

public class CellEditorMulticaster implements TableCellEditor {
    private DefaultTableCellEditor textEditor;
    private DefaultTableCellEditor currentEditor;

    public CellEditorMulticaster() {
        firstEditor = new DefaultTableCellEditor(new JTextField());
    }

    Component getTableCellEditorComponent(JTable table, Object value,
                                      boolean isSelected,
                                      int row, int column) {
        PropertiesTableModel model = (PropertiesTableModel) table.getModel();
        List<Object> values = model.getPossibleValues(row, column);
        if (values != null) {
            currentEditor = new DefaultCellEditor(new JComboBox(values.toArray()));
        } else {
            currentEditor = textEditor;
        }
        return currentEditor.getTableCellEditorComponent(table, value,
               isSelected, row, column);
    }

    public Object getCellEditorValue() {
        return currentEditor.getCellEditorValue();
    }

    public boolean isCellEditable(EventObject anEvent) {
        JTable table = (JTable) anEvent.getSource;
        int row, col;
        if (anEvent instanceof MouseEvent) {
            MouseEvent evt = (MouseEvent) anEvent;
            row = table.rowAtPoint(evt.getPoint());
            col = table.columnAtPoint(evt.getPoint());
        } else {
            row = table.getSelectedRow();
            col = table.getSelectedColumn();
        }
        PropertiesTableModel model = (PropertiesTableModel) table.getModel();
        List<Object> values = model.getPossibleValues(row, column);
        if (values != null) {
            return true;
        } else {
            return textEditor.isCellEditable(anEvent);
        }
    }

    public boolean shouldSelectCell(EventObject anEvent) {
        return true;
    }

    public boolean stopCellEditing() {
        return currentEditor.stopCellEditing();
    }

    public void  cancelCellEditing() {
        currentEditor.cancelCellEditing();
    }

    // same pattern for another methods (delegate to currentEditor)
}   
公共类CellEditorMulticaster实现TableCellEditor{
私有DefaultTableCellEditor文本编辑器;
私有DefaultTableCellEditor当前编辑器;
公共CellEditorMulticaster(){
firstEditor=新的DefaultTableCellEditor(新的JTextField());
}
组件getTableCellEditorComponent(JTable表、对象值、,
他当选了,,
整数行,整数列){
PropertiesTableModel model=(PropertiesTableModel)table.getModel();
列表值=model.getPossibleValues(行、列);
如果(值!=null){
currentEditor=新的DefaultCellEditor(新的JComboBox(values.toArray());
}否则{
currentEditor=文本编辑器;
}
返回currentEditor.getTableCellEditorComponent(表、值、,
i选定,行,列);
}
公共对象getCellEditorValue(){
返回currentEditor.getCellEditorValue();
}
公共布尔值isCellEditable(EventObject anEvent){
JTable table=(JTable)anEvent.getSource;
int row,col;
if(MouseEvent的事件实例){
MouseEvent evt=(MouseEvent)anEvent;
行=table.rowAtPoint(evt.getPoint());
col=table.columnAtPoint(evt.getPoint());
}否则{
行=表。getSelectedRow();
col=table.getSelectedColumn();
}
PropertiesTableModel model=(PropertiesTableModel)table.getModel();
列表值=model.getPossibleValues(行、列);
如果(值!=null){
返回true;
}否则{
返回textEditor.isCellEditable(anEvent);
}
}
公共布尔值shouldSelectCell(EventObject anEve