I have a table and have an combobox on each row in same column index. So the problem is I have I try to set same value on each row when I select an value on combobox. I read previously asked question about combobox on table cell editor and I tryed to implement answer to my code but I couldnt managed. I can see what I wished but My action listener sent many exceptions on behind. How to fix it?
the code for this table
tblRadars= new JTable();
tblRadars.setModel(new DefaultTableModel(
new Object[][] {},
new String[] {
"Radar", "Multicast IP", "Port", "Period", "Size", "Start", "Stop"
}
) {
/**
*
*/
private static final long serialVersionUID = 1L;
boolean[] columnEditables = new boolean[] {
false, false, false, true, false, true, true};
public boolean isCellEditable(int row, int column) {
if (column == PERIOD_COLUMN_INDEX ){
return ((JButton)getValueAt(row, START_COLUMN_INDEX)).isEnabled();
}
return columnEditables[column];
}
});
TableColumn periodColumn = tblRadars.getColumnModel().getColumn(3);
final JComboBox comboBox = new JComboBox(new Object[]{"1 min","30 min","1 hr" , "2 hr","4 hr","6 hr", "12 hr"});
comboBox.setSelectedIndex(-1);
DefaultCellEditor ed=new DefaultCellEditor(comboBox);
periodColumn.setCellEditor(ed);
and my funny action listener is
ed.addCellEditorListener(new CellEditorListener() {
@Override
public void editingStopped(ChangeEvent e) {
// TODO Auto-generated method stub
String value=(String)tblRadars.getValueAt(tblRadars.getSelectedRow(), PERIOD_COLUMN_INDEX);
int row=tblRadars.getRowCount();
for(int i=0;i<row;i++){
tblRadars.setValueAt(value, i, PERIOD_COLUMN_INDEX);
}
}
From now thank you for your help.
I am really fool . Sometimes answer is really simple but we always see our problem big.
ActionListener comboBoxAction = new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("period clicked.");
int row=tblRadars.getSelectedRow();
int comboboxColumn=3 ;
String selectedvalue=comboBox.getSelectedItem().toString();
for(int i=0;i<tblRadars.getRowCount();i++){
tblRadars.setValueAt(selectedvalue, i, comboboxColumn);
}
((DefaultTableModel)tblRadars.getModel()).fireTableDataChanged();
}
};
then add this action to combobox action listener. :)