1

I need to put a checkbox in the column Present to mark when a guest arrives (it's a application to manage events)

String[] columns = {"Name","Present"};
Object[][] listInv = {
{"Giacomo Guilizzoni", false},
{"Guido Jack Guilizzoni", true},
{"Marco Botton", false},
{"Mariah Maclachlan", true},
{"Valierie Liberty", true}
};

DefaultTableModel dtm = new DefaultTableModel();
dtm.setDataVector(listInv, columns);

Jable tabla = new JTable(dtm);
TableColumn tcolumnas;

tcolumnas = tabla.getColumnModel().getColumn(0);
tcolumnas.setPreferredWidth(300);

jScroll = new JScrollPane(tabla);
tabla.setFillsViewportHeight(true);

I override the method getColumnsClass:

public Class getColumnClass(int columna) {
return Boolean.class;
}

Instead of show a JCheckBox is showing a string "true" or "false" What i'm doing wrong?


Edit:

it was as simple as this:

TableColumn tcolumnas = tabla.getColumnModel().getColumn(1);
columnas.setCellRenderer(tabla.getDefaultRenderer(Boolean.class));
tcolumnas.setCellEditor(tabla.getDefaultEditor(Boolean.class));
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
Aikanáro
  • 867
  • 3
  • 20
  • 42
  • 1
    Based on the posted code you are NOT overriding the getColumnClass(...) method. You can override the method of JTable or DefaultTbleModel. Your code does not do this. – camickr Nov 26 '11 at 19:48
  • 3
    See also [How to make Jtable column contain checkboxes?](http://stackoverflow.com/questions/2901470/how-to-make-jtable-column-contain-checkboxes) – trashgod Nov 26 '11 at 20:50
  • t was as simple as this: `TableColumn tcolumnas = tabla.getColumnModel().getColumn(1); tcolumnas.setCellRenderer(tabla.getDefaultRenderer(Boolean.class)); tcolumnas.setCellEditor(tabla.getDefaultEditor(Boolean.class));` I don't understand why in other tutorials, people talk about inherit, implement interfaces and override methods... – Aikanáro Nov 27 '11 at 00:18
  • 2
    @Aikanáro, Did you notice the the formatting is lost when you place code in a comment? Update your question to post the code you are using so it is readable. Overriding the getColumnClass() method is easier and more reliable. All the code is located in one place because it becomes part of the TableModel. So the business logic is in one place. – camickr Nov 27 '11 at 03:27
  • @Aikanáro if you use code in your comments please use EITHER Backticks OR indentation not BOTH. It messes the display. (See my edit for details) – Angelo Fuchs Feb 28 '12 at 20:26

0 Answers0