I have a table filled with comboboxes,
What I want to do is the folowing: If an element in a combobox is selected, then automatically change the selection on another combo (the reciprocal).
I can't figureout a way to do it. Here I post an image describing what I want to do.
The code of my table is the following:
public void createCriteriaMatrix(){
jTableCriteria = new JTable();
// COLUMNS
String[] column = new String[problem.getCriteria()+1];
for(int i=0; i<problem.getCriteria()+1; i++){
column [i] = " "+i;
if(i==0){
column [i] = " ";
}
}
// DATA CELLS
String [][] data = new String[problem.getCriteria()][problem.getCriteria()+1];
for(int j=0; j<problem.getCriteria()+1; j++){
for(int i=0; i<problem.getCriteria(); i++){
data [i][j]=" ";
if(j==0){
data [i][j] = " "+(i+1);
}
}
}
//SOME TABLE FORMAT
DefaultTableModel model = new DefaultTableModel(data , column ){
@Override
public boolean isCellEditable(int row, int column) {
return column != 0;
}
};
//Calling to RenderCells() to format cell colors
jTableCriteria.setDefaultRenderer (Object.class, new RenderCells());
jTableCriteria.setModel(model);
jTableCriteria.getTableHeader().setReorderingAllowed(false);
this.placeCombosTable();
}
public void placeCombosTable(){
for(int i=0; i<=problem.getCriteria(); i++){
for(int j=0; j<=problem.getCriteria(); j++){
TableColumn weighting= jTableCriteria.getColumnModel().getColumn(i);
JComboBox comboBox = new JComboBox();
comboBox.addItem("1");
comboBox.addItem("2");
comboBox.addItem("3");
comboBox.addItem("4");
comboBox.addItem("5");
comboBox.addItem("6");
comboBox.addItem("7");
comboBox.addItem("1/2");
comboBox.addItem("1/3");
comboBox.addItem("1/4");
comboBox.addItem("1/5");
comboBox.addItem("1/6");
comboBox.addItem("1/7");
if(i==j){
comboBox.setSelectedIndex(0);
}
weighting.setCellEditor(new DefaultCellEditor(comboBox));
}
}
}