我在我的专栏中创建了 JComboBox,它工作正常。当我尝试在同一列中再添加一个编辑器时,出现了问题。场景,用户需要从 ComboBox 中选择值作为备注。如果他们选择其他,则组合框下方应出现另一个文本框供用户键入。
组合框代码
TableColumn col5 = jTable1.getColumnModel().getColumn(4);
String[] options = new String[]{"Font Issue","Text Issue","Image Issue","AI Issue","Others"};
JComboBox combo1 = new JComboBox(options);
JComboBox combo2 = new JComboBox(options);
col5.setCellEditor(new DefaultCellEditor(combo1));
col5.setCellRenderer(new ComboBoxRenderer(combo2));
combo2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox nameCombo = (JComboBox)e.getSource();
String newSelection = (String)nameCombo.getSelectedItem();
if(newSelection.equalsIgnoreCase("others"))
{
}
}
});
当我再添加一位编辑器时。
MyTableCellEditor textEditor = new MyTableCellEditor();
col5.setCellEditor(textEditor);
它会覆盖下拉列表。我想要有这样的东西。
最佳答案
Swing 编辑器旨在占据单个单元格的空间。如果您想显示具有两个组件的面板,那么您将需要创建一个弹出编辑器。阅读 Swing 教程中关于 Using Other Editors 的部分有关如何执行此操作的示例。
关于java - 在一个单元格中添加多个编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17985166/