-3

I am using a JTable and I modify its model to contain JComboBox items. I do that this way:

JComboBox comboBox = new javax.swing.JComboBox(substituteNames.split(","));
comboBox.setSelectedIndex(0);
editors.add(new javax.swing.DefaultCellEditor(comboBox));

where substituteNames is "1,2,3,4", for example.

However, in my JTable initially the JComboBox elements' selected item is empty, however, there is no empty element in the JComboBox. After I select something else, the empty element disappears. Why is this happening, how could I resolve that the selected item of my JComboBox is the first element initially?

Edit: This is how I edit the table model.

        RowEditorModel rm = new RowEditorModel();
        issue.setRowEditorModel(rm);
        for (int issueIndex = 0; issueIndex < rowIndexes.size(); issueIndex++)
        {
            rm.addEditorForRow(rowIndexes.get(issueIndex).intValue(), editors.get(issueIndex));
        }

RowEditorModel is a class:

public class RowEditorModel
 {
      private Hashtable data;
      public RowEditorModel()
      {
          data = new Hashtable();
      }
     public void addEditorForRow(int row, TableCellEditor e )
     {
         data.put(new Integer(row), e);
     }
     public void removeEditorForRow(int row)
     {
         data.remove(new Integer(row));
     }
     public TableCellEditor getEditor(int row)
     {
         return (TableCellEditor)data.get(new Integer(row));
     }
 }

issue is of type JTableX, which is a transitive descendant of 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);
      }
  }
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175

1 Answers1

4

You can't just set the index of the combo box because the same editor is used for all rows in the column.

So, when you edit a cell in a JTable that uses a combo box for the editor, the selected item of the combo box is set to be the value that is contained in the TableModel for the cell currently being edited.

how could I resolve that the selected item of my JComboBox is the first element initially?

You need to update the TableModel for all cells to contain that value.

Read the section from the Swing tutorial on Using a ComboBox as an Editor, for a working example. Note how the TableModel contains different values for each row.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • See also this related [example](http://stackoverflow.com/a/3256602/230513) that selects an editor by row. – trashgod Jun 26 '13 at 17:11
  • Trashgod, I am doing something like that already. Camickr, the comboBox is created before it is passed to the constructor of DefaultCellEditor and it has all the needed elements, so I am puzzled why should I update the TableModel to solve my issue, as it already contains in the current version all the needed elements, but the initially selected element is empty value. – Lajos Arpad Jun 26 '13 at 17:20
  • The job of the editor is to display the value found in the TableModel. Every time the editor is invoked the current value in the table model is used to set the selected object of the combo box. Look at the tutorial and play with the example. Understand the basics of a simple table before you try playing with a different combo box editor for every row. – camickr Jun 26 '13 at 17:38
  • Well, thank you, but I have already viewed that. I have a problem which was mentioned in the question, I was hoping that somebody points me to the right direction. I will resolve my issue and will let you know what was the solution. Anyway, thanks for the effort. – Lajos Arpad Jun 26 '13 at 17:43
  • `but I have already viewed that` - So when you run the example do you see an empty selection??? The answer is no, because the TableModel is initialized properly. `I was hoping that somebody points me to the right direction` - You have been pointed in the right direction. You can NOT set the selected item of the combo box directly. The selected object comes from the TableModel. Set the value in the TableModel. What don't you understand about that statement? – camickr Jun 26 '13 at 18:00
  • "Understand the basics of a simple table before you try playing with a different combo box editor for every row." That's a step backwards which is not the right direction. "You can't just set the index of the combo box because the same editor is used for all rows in the column." No, I have different combo box editors for each rows. "You need to update the TableModel for all cells to contain that value." That's true, but it was unclear for me until I realized it myself. I upvote your answer for being correct, but will not accept it as it was not clear enough, at least for me. – Lajos Arpad Jun 26 '13 at 18:29
  • `You can NOT set the selected item of the combo box directly. The selected object comes from the TableModel. Set the value in the TableModel.` Again I ask what is NOT clear about that statement. It was clear enough for the others the up voted it. And it was also clear enough so that nobody else posted another suggestion. Good luck getting help in the future when you don't appreciate the help received. – camickr Jun 26 '13 at 20:08
  • 1
    @LajosArpad You seem to have in your head the way you think that this *"should"* work, which is generally wrong, and are expecting an answer that supports that concept, which you won't find. *"No, I have different combo box editors for each rows"* is the wrong idea. Each column has only ONE editor, each row of that column shares that editor. The JTable passes the value of the cell to it when it prepares the editor and asks for the value when it stops editing. If you find yourself wanting to extend JTable to facilitate some function, then you've probably done something wrong. – MadProgrammer Jun 26 '13 at 20:33