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);
}
}