I added a Swing JComboBox to a JTable, but the label of my first item is always javax.swing.JComboBox(...

What am I doing wrong?
UPDATE : this is my code :
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
public class TableSelection extends JPanel {
private DefaultTableModel model = new DefaultTableModel();
private JTable table = new JTable(model);
public TableSelection() {
model = (DefaultTableModel) table.getModel();
ArrayList<String> labels = new ArrayList<String>();
labels.add("");
for (int i = 1; i < 10 + 1; i++) {
labels.add("" + (i - 1));
}
model.addColumn("Column");
model.addColumn("Column2");
JComboBox<String> jcombo1 = new JComboBox<String>();
jcombo1.setModel(new DefaultComboBoxModel(labels.toArray()));
jcombo1.setBackground(Color.WHITE);
jcombo1.setSelectedIndex(1);
DefaultCellEditor editor = new DefaultCellEditor(jcombo1);
table.getColumnModel().getColumn(0).setCellEditor(editor);
model.addRow(new JComboBox[]{jcombo1});
table.setPreferredScrollableViewportSize(new Dimension(560, 300));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.add(new JScrollPane(table));
table.setRowSelectionInterval(0, 0);
}
public static void main(String[] args) {
JFrame f = new JFrame("TableSelection");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new TableSelection());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
The essential problem is here:
model.addRow(new JComboBox[]{combo});
Don't add components to the table model. Instead, let the renderer handle the job, using "a label that displays the object's string value." Your initial label, an empty String, is shown below.
model.addRow(new String[]{labels.get(0)});

In addition:
Code to the interface, e.g. List<String> labels = new ArrayList<>()
Start on the event dispatch thread.
Code as tested:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
public class TableSelection extends JPanel {
private final DefaultTableModel model = new DefaultTableModel();
private final JTable table = new JTable(model);
public TableSelection() {
List<String> labels = new ArrayList<>();
labels.add("");
for (int i = 1; i < 10 + 1; i++) {
labels.add("" + (i - 1));
}
model.addColumn("Column 1");
model.addColumn("Column 2");
JComboBox<String> combo = new JComboBox<>();
combo.setModel(new DefaultComboBoxModel(labels.toArray()));
combo.setBackground(Color.WHITE);
combo.setSelectedIndex(1);
DefaultCellEditor editor = new DefaultCellEditor(combo);
table.getColumnModel().getColumn(0).setCellEditor(editor);
//model.addRow(new JComboBox[]{combo});
model.addRow(new String[]{labels.get(0)});
table.setPreferredScrollableViewportSize(new Dimension(320, 120));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.add(new JScrollPane(table));
table.setRowSelectionInterval(0, 0);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame("TableSelection");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new TableSelection());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
The problem is that you're adding the JComboBox to your table model instead of the actual data.
Instead of this:
model.addRow(new JComboBox[]{jcombo1});
Do this:
model.addRow(new Object[]{labels});
There's a full example here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With