我向JComboBox
添加了Swing JTable
,但我的第一个项目的标签始终为javax.swing.JComboBox(...
我做错了什么?
更新:这是我的代码:
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);
}
}
答案 0 :(得分:4)
基本问题在于:
model.addRow(new JComboBox[]{combo});
不要将组件添加到表模型中。相反,让renderer使用“显示对象字符串值的标签”来处理作业。您的初始标签为空String
,如下所示。
model.addRow(new String[]{labels.get(0)});
另外:
接口的代码,例如List<String> labels = new ArrayList<>()
经过测试的代码:
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);
});
}
}
答案 1 :(得分:3)
问题是您要将JComboBox
添加到表格模型而不是实际数据中。
而不是:
model.addRow(new JComboBox[]{jcombo1});
这样做:
model.addRow(new Object[]{labels});
有一个完整的示例here