I am trying to make a combobox appear in just one row in a column.
The lines
TableColumn tableColumn = table.getColumnModel().getColumn(1);
tableColumn.setCellEditor(new DefaultCellEditor(comboBox));
...make the combobox to appear in all cells in the specified column.
I have also tried
table.getModel().setValueAt(new DefaultCellEditor(comboBox),1,1);
...but it doesn´t make the comboxo appear in the specified row.
How do I get the combobox to appear in just one row in a column?
The whole code is as follows:
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.DefaultTableModel;
import java.awt.GridLayout;
class TableCell extends JFrame
{
JComboBox comboBox = new JComboBox();
DefaultTableModel model = new DefaultTableModel();
JTable table;
TableCell()
{
comboBox.addItem("Tennis");
comboBox.addItem("Soccer");
comboBox.addItem("Baseball");
comboBox.addItem("Basketball");
model.addColumn("First Name");
model.addColumn("Last Name");
String[] row1 = { "Thomas", "Aquinas", "1225-1274" };
String[] row2 = { "Mother", "Teresa", "1225-1274" };
model.addRow(row1);
model.addRow(row2);
table = new JTable(model);
TableColumn tableColumn = table.getColumnModel().getColumn(1);
add(new JScrollPane(table));
tableColumn.setCellEditor(new DefaultCellEditor(comboBox));
//DefaultCellEditor dce = new DefaultCellEditor(comboBox);
table.getModel().setValueAt(new DefaultCellEditor(comboBox),1,1);
//Display the window.
setSize(200,200);
setVisible(true);
}
public static void main(String[] args)
{
new TableCell();
}
}