Java 为什么我最初在JComboBox对象中选择了一个空项?

Java 为什么我最初在JComboBox对象中选择了一个空项?,java,swing,jtable,jcombobox,Java,Swing,Jtable,Jcombobox,我使用的是JTable,我修改了它的模型以包含JComboBox项。我这样做: JComboBox comboBox = new javax.swing.JComboBox(substituteNames.split(",")); comboBox.setSelectedIndex(0); editors.add(new javax.swing.DefaultCellEditor(comboBox)); 例如,其中取代烯为“1,2,3,4” 但是,在我的JTable中,最初JComboBoxe

我使用的是
JTable
,我修改了它的模型以包含
JComboBox
项。我这样做:

JComboBox comboBox = new javax.swing.JComboBox(substituteNames.split(","));
comboBox.setSelectedIndex(0);
editors.add(new javax.swing.DefaultCellEditor(comboBox));
例如,其中取代烯为“1,2,3,4”

但是,在我的
JTable
中,最初
JComboBox
elements'所选项目为空,但是
JComboBox
中没有空元素。选择其他内容后,空元素将消失。为什么会发生这种情况,我如何确定我的
JComboBox
的选定项最初是第一个元素

编辑: 这就是我编辑表格模型的方式

        RowEditorModel rm = new RowEditorModel();
        issue.setRowEditorModel(rm);
        for (int issueIndex = 0; issueIndex < rowIndexes.size(); issueIndex++)
        {
            rm.addEditorForRow(rowIndexes.get(issueIndex).intValue(), editors.get(issueIndex));
        }
问题类型为JTableX,它是JTable的可传递后代:

 public class JTableX extends MiniTable
  {
        private boolean[][] editable_cells = null; // 2d array to represent rows and columns

        @Override
        public boolean isCellEditable(int row, int col) { // custom isCellEditable function
            if (editable_cells == null)
            {
                editable_cells = new boolean[getModel().getRowCount()][getModel().getColumnCount()];
            }
           return this.editable_cells[row][col];
        }

        public void setCellEditable(int row, int col, boolean value) {
            if (editable_cells == null)
            {
                editable_cells = new boolean[getModel().getRowCount()][getModel().getColumnCount()];
            }
            this.editable_cells[row][col] = value; // set cell true/false
            ((DefaultTableModel)getModel()).fireTableCellUpdated(row, col);
        }
      protected RowEditorModel rm;

      public JTableX()
      {
          super();
          rm = null;
      }

      public JTableX(TableModel tm)
      {
          super(tm);
          rm = null;
      }

      public JTableX(TableModel tm, TableColumnModel cm)
      {
          super(tm,cm);
          rm = null;
      }

      public JTableX(TableModel tm, TableColumnModel cm, ListSelectionModel sm)
      {
          super(tm,cm,sm);
          rm = null;
      }

      public JTableX(int rows, int cols)
      {
          super(rows,cols);
          rm = null;
      }

      public JTableX(final Vector rowData, final Vector columnNames)
      {
          super(rowData, columnNames);
          rm = null;
      }

      public JTableX(final Object[][] rowData, final Object[] colNames)
      {
          super(rowData, colNames);
          rm = null;
      }

      // new constructor
      public JTableX(TableModel tm, RowEditorModel rm)
      {
          super(tm,null,null);
          this.rm = rm;
      }

      public void setRowEditorModel(RowEditorModel rm)
      {
          this.rm = rm;
      }

      public RowEditorModel getRowEditorModel()
      {
          return rm;
      }

      public TableCellEditor getCellEditor(int row, int col)
      {
          TableCellEditor tmpEditor = null;
          if (rm!=null)
              tmpEditor = rm.getEditor(row);
          if (tmpEditor!=null)
              return tmpEditor;
          return super.getCellEditor(row,col);
      }
  }

不能只设置组合框的索引,因为列中的所有行都使用相同的编辑器

因此,在使用组合框作为编辑器的JTable中编辑单元格时,组合框的选定项将设置为当前正在编辑的单元格的TableModel中包含的值

如何确定JComboBox的选定项最初是第一个元素

您需要更新所有单元格的TableModel以包含该值


阅读上的Swing教程中的部分,以获得一个工作示例。请注意TableModel如何为每一行包含不同的值。

为了更快地获得更好的帮助,请包含一个。我已经编辑了我的问题,希望我现在提供足够的信息。另请参阅这篇按行选择编辑器的相关文章。垃圾天啊,我已经在做类似的事情了。Camickr,comboBox是在传递给DefaultCellEditor的构造函数之前创建的,它包含了所有需要的元素,因此我不明白为什么要更新TableModel来解决我的问题,因为它在当前版本中已经包含了所有需要的元素,但是最初选择的元素是空值。编辑器的任务是显示在TableModel中找到的值。每次调用编辑器时,表模型中的当前值用于设置组合框的选定对象。查看教程并使用示例。在尝试为每一行使用不同的组合框编辑器之前,请先了解简单表格的基本知识。好的,谢谢,但我已经看过了。我有一个问题在问题中提到,我希望有人能给我指出正确的方向。我将解决我的问题,并让您知道解决方案是什么。无论如何,谢谢你的努力。@LajosArpad你的脑子里似乎有你认为这个“应该”起作用的方式,这通常是错误的,并且期待着一个支持这个概念的答案,而这是你找不到的。“不,我对每行有不同的组合框编辑器”是错误的想法。每列只有一个编辑器,该列的每一行共享该编辑器。JTable在准备编辑器时将单元格的值传递给它,并在停止编辑时请求该值。如果您发现自己想要扩展JTable以促进某些功能,那么您可能做错了什么。
 public class JTableX extends MiniTable
  {
        private boolean[][] editable_cells = null; // 2d array to represent rows and columns

        @Override
        public boolean isCellEditable(int row, int col) { // custom isCellEditable function
            if (editable_cells == null)
            {
                editable_cells = new boolean[getModel().getRowCount()][getModel().getColumnCount()];
            }
           return this.editable_cells[row][col];
        }

        public void setCellEditable(int row, int col, boolean value) {
            if (editable_cells == null)
            {
                editable_cells = new boolean[getModel().getRowCount()][getModel().getColumnCount()];
            }
            this.editable_cells[row][col] = value; // set cell true/false
            ((DefaultTableModel)getModel()).fireTableCellUpdated(row, col);
        }
      protected RowEditorModel rm;

      public JTableX()
      {
          super();
          rm = null;
      }

      public JTableX(TableModel tm)
      {
          super(tm);
          rm = null;
      }

      public JTableX(TableModel tm, TableColumnModel cm)
      {
          super(tm,cm);
          rm = null;
      }

      public JTableX(TableModel tm, TableColumnModel cm, ListSelectionModel sm)
      {
          super(tm,cm,sm);
          rm = null;
      }

      public JTableX(int rows, int cols)
      {
          super(rows,cols);
          rm = null;
      }

      public JTableX(final Vector rowData, final Vector columnNames)
      {
          super(rowData, columnNames);
          rm = null;
      }

      public JTableX(final Object[][] rowData, final Object[] colNames)
      {
          super(rowData, colNames);
          rm = null;
      }

      // new constructor
      public JTableX(TableModel tm, RowEditorModel rm)
      {
          super(tm,null,null);
          this.rm = rm;
      }

      public void setRowEditorModel(RowEditorModel rm)
      {
          this.rm = rm;
      }

      public RowEditorModel getRowEditorModel()
      {
          return rm;
      }

      public TableCellEditor getCellEditor(int row, int col)
      {
          TableCellEditor tmpEditor = null;
          if (rm!=null)
              tmpEditor = rm.getEditor(row);
          if (tmpEditor!=null)
              return tmpEditor;
          return super.getCellEditor(row,col);
      }
  }