Reputation: 73
I'm stuck on the following problem and can not find what I'm doing wrong. My problem is to replace the default editor of a cell of a JTable and use a ComboBox instead. My code appears to work correctly, but does not cause the expected effect. After much reflection, and after verifying that the solution proposed by Oracle in their tutorial works, I decided to lighten my JTable code removing my own table model and leaving the default model, The surprise was that it works perfectly.
private void establecerColumna() {
//Cuando este sea deba ser distinto del que se establece por defecto.
JComboBox c = new JComboBox();
c.addItem("0000");
c.addItem("1111");
c.addItem("2222");
tblCuentas.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(c));
}
This code block works perfectly with any kind of cell editor, not only with a ComboBox.
But things change when I use my own Class to control de JTable.
private void establecerModeloTabla() {
String[] colTitles = {"Nº Orden", "Entidad", "Sucursal", "DC", "Cuenta", "Iban", "Bic", "Activa"};
String[] colFields = {"IdCuenta", "Entidad", "Oficina", "Dc", "Cuenta", "Iban", "Bic", "Activa"};
TableController modelo ;
modelo= new TableController(new EmpresaCuentas(emp.getConexion()));
tblCuentas.setModel(modelo);
modelo.setColumnFields(colFields);
modelo.setColumNames(colTitles);
//Establecemos el componente de edicion que me interesa para cada campo de la tabla
//Cuando este sea deba ser distinto del que se establece por defecto.
JComboBox c = new JComboBox();
c.addItem("0000");
c.addItem("1111");
c.addItem("2222");
modelo.setCellEditor(tblCuentas.getColumnModel().getColumn(1), c, "test title");
}
the setCellEditor method is the next:
public void setCellEditor(TableColumn tc, JComboBox colType,String toolTipText){
tc.setCellEditor(new DefaultCellEditor(colType));
// establecemos el renderer del campo.
if (toolTipText!=null && toolTipText.length()>0){
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText(toolTipText);
tc.setCellRenderer(renderer);
}
fireTableChanged(null);
}
What is what makes my class TableController?
Basically connects to a dependent table and links it with the JTable component. This a Class that extends the AbstractTableModel class and there is where the control model is established. All that part is working properly, adds, deletes and updates the established fields of the table, but I have to stick to the default editors. For other logical fields checkbox is set, but for everyone else I can not use the JComboBox or JFormattedTextField components or even limit the introduction of characters, which does work when not use my class derived from an AbstractTableModel class.
Any suggestion?
Upvotes: 0
Views: 427
Reputation: 324207
fireTableChanged(null);
Don't invoke the above code. It is the job of the TableModel
to invoke that method as required.
In your case it causes a problem because JTable
will recreate the TableColumnModel
which will reset the default renderers and editors for the table.
Upvotes: 2