EDIT NOW WITH MCVE:
I want to view one JTable cell as a JComboBox. I tried it with the Oracle Tutorial and it doesn't work. The tutorial doesn't say anything about how you have to change your table model to get it working. So here's the code:
TableModel:
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
public class TableModel extends AbstractTableModel {
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return 2;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return 2;
}
@Override
public Object getValueAt(int row, int col) {
if (row == 0) {
return 2;
} else if (col == 1 ) {
return 1;
} else {
return null;
}
}
}
And the view to show the components:
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.table.TableColumn;
public class View extends JFrame {
public View() {
super();
TableModel tableModel = new TableModel();
JTable testTable = new JTable(tableModel);
JScrollPane scrollpane = new JScrollPane(testTable);
// scrollpane.setOpaque(false);
// scrollpane.getViewport().setOpaque(false);
JPanel testTablePanel = new JPanel();
testTablePanel.setBorder(
BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Testkonfiguration"));
testTablePanel.setLayout(new BorderLayout());
testTablePanel.add(scrollpane);
TableColumn sportColumn = testTable.getColumnModel().getColumn(1);
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.addItem("Speed reading");
comboBox.addItem("Teaching high school");
comboBox.addItem("None");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
add(scrollpane);
pack();
setVisible(true);
}
public static void main(String[] args) {
new View();
}
}
What do I have to change in the tablemodel to get it working ?
In your example, you don't make the 1 column editable. You first need to do this, such as in your model:
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
Also your model doesn't handle data well, has no way to get the data in fact and in fact is fixed, so that the editor can have no possible effect. You need to override setValueAt and have it update the model's nucleus.
For example using your trivial example:
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
public class View extends JFrame {
public View() {
super();
TableModel tableModel = new TableModel();
JTable testTable = new JTable(tableModel);
JScrollPane scrollpane = new JScrollPane(testTable);
// scrollpane.setOpaque(false);
// scrollpane.getViewport().setOpaque(false);
JPanel testTablePanel = new JPanel();
testTablePanel.setBorder(BorderFactory
.createTitledBorder(BorderFactory.createEtchedBorder(), "Testkonfiguration"));
testTablePanel.setLayout(new BorderLayout());
testTablePanel.add(scrollpane);
TableColumn sportColumn = testTable.getColumnModel().getColumn(1);
JComboBox<String> comboBox = new JComboBox<>();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.addItem("Speed reading");
comboBox.addItem("Teaching high school");
comboBox.addItem("None");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(scrollpane);
pack();
setVisible(true);
}
public static void main(String[] args) {
new View();
}
}
class TableModel extends AbstractTableModel {
Object[][] innerModel = new Object[][]{{2, 1}, {2, null}};
public TableModel() {
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public int getRowCount() {
return innerModel.length;
}
@Override
public Object getValueAt(int row, int col) {
return innerModel[row][col];
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
innerModel[rowIndex][columnIndex] = aValue;
fireTableCellUpdated(rowIndex, columnIndex);
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
}