我的桌子上有 JComboBox。如果用户从 ComboBox 中选择“其他”,我需要隐藏表中的第 3 列。
代码
final TableColumn col5 = jTable1.getColumnModel().getColumn(4);
col5.setPreferredWidth(150);
final String EDIT = "edit";
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) {
String newSelection = col5.getCellEditor().getCellEditorValue().toString();
String strOthersRemark = "";
if (newSelection.equalsIgnoreCase("others")) {
jTable1.removeColumn(jTable1.getColumnModel().getColumn(3));
}
}
});
代码工作正常,但有一个小问题。当用户选择其他人时,它会删除整个列而不是行。例如
Row|Column1 | Column2 | Column3 | Column4 |
1 | Test11 | Test12 | Test13 | Test14 |
2 | Test21 | Test22 | Test23 | Test24 |
3 | Test31 | Test32 | Test33 | Others |
当用户选择 Column4 因为Others
它应该隐藏而Test33
不是整个Column3
. 我的代码删除了整个Column3
. Test33
如果只想隐藏怎么办