I'm trying to get a jtable to contain combo boxes for one of it's columns but it doesn't work, it just appears as a normal table cells. at the moment i'm following oracle's example: http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableRenderDemoProject/src/components/TableRenderDemo.java
I tried all the topics posted here about this and various methods as well, i got it to work once but lost it after i tried a different method. What am i doing wrong? I'm not including all the code, it's way too long, just the relevant part. clientsTable has been declared before as a jTable.
// Define Table model for clients table
class ClientsTableModel extends DefaultTableModel {
public ClientsTableModel(Vector<Vector<String>> clientsDataVector,
Vector<String> clientColumNamesVector) {
super(clientsDataVector, clientColumNamesVector);
}
@Override
public int getColumnCount() {
return clientColumNames.length;
}
@Override
public int getRowCount() {
return clientsDataVector.size();
}
@Override
public String getValueAt(int row, int column) {
return clientsDataVector.get(row).get(column);
}
@Override
public void setValueAt(Object aValue, int row, int column) {
clientsDataVector.get(row).set(column, (String) aValue);
}
@Override
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
// create table model and add to clients table
clientColumNames = new String[] { "ID", "Name", "Type", "Address",
"Email", "Phone", "Comment" };
clientColumNamesVector = new Vector<String>(
Arrays.asList(clientColumNames));
clientsDataVector = new Vector<Vector<String>>(1, 1);
clientsTableModel = new ClientsTableModel(clientsDataVector,
clientColumNamesVector);
clientsTableModelEvent = new TableModelEvent(clientsTableModel);
clientsTableModel.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent arg0) {
}
});
// create clients table and set type column to be combo box
String[] clientTypes = { "REGULAR", "GOLD", "PLATINUM" };
clientsTable = new JTable(clientsTableModel);
clientsTable.setAutoCreateRowSorter(true);
clientsTable.setFillsViewportHeight(true);
JComboBox clientsTypeComboBox = new JComboBox(clientTypes);
TableColumn clientsTypeColumn = clientsTable.getColumnModel().getColumn(2);
clientsTypeColumn.setCellEditor(new DefaultCellEditor(clientsTypeComboBox));
DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
// create client scroll pane
JScrollPane clientsScrollPane = new JScrollPane(clientsTable);
GridBagConstraints gbc_clientsScrollPane = new GridBagConstraints();
gbc_clientsScrollPane.insets = new Insets(0, 0, 5, 0);
gbc_clientsScrollPane.fill = GridBagConstraints.BOTH;
gbc_clientsScrollPane.gridx = 0;
gbc_clientsScrollPane.gridy = 0;
viewClientsPanel.add(clientsScrollPane, gbc_clientsScrollPane);
thank you for your answers, i actually managed to find what was wrong. i endded up using tableCellEditor and u made my combobox to implement tablecellrenderer, which i tried before but the main thing that i didn't do is to override jtable getcellrendere/editor methods: clientsTable = new JTable(clientsTableModel){