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);
It occures, because you have default renderer for your column. When you start to edit, column shows as
JComboBox, because you setDefaultCellEditorwithJComboBox. If you want to render cells asJComboBoxalways, you can implement aTableCellRendererfor custom view of cell, read tutorial for that. Here is simple example for you:and its look like next: