I have JComboBox in my table. If user selected "Others" from the ComboBox i need to hide column number 3 in the table.
Code
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));
}
}
});
The code working fine but with one small issue. When user select others it removed the entire column instead the row.For an example
Row|Column1 | Column2 | Column3 | Column4 |
1 | Test11 | Test12 | Test13 | Test14 |
2 | Test21 | Test22 | Test23 | Test24 |
3 | Test31 | Test32 | Test33 | Others |
When user select Column4 as Others
it should hide the Test33
, not entire Column3
. My code remove entire Column3
. What should I do if I want to hide Test33
only
You're removing the column:
jTable1.removeColumn(jTable1.getColumnModel().getColumn(3));
Instead you should change the value at certain cell.
Use this method instead: table.setValueAt()
. Java doc: setValueAt
In your example:
jTable1.setValueAt("", 3, 3);