Conditional JComboBox inside JTable - How to Automatically change values

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. Matrix automatic JComboboxUpdate

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));
        }

    }             
}

Upvotes: 1

Views: 942

Answers (3)

I solved it with an Array for the reciprocal pairwise comparison (to compare and then set the other combo with the correct value), a Listener for the combobox and a setValueAt() instead of a setSelectedItem(). The code is the following:

 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);
            }
            //SCALE
            final String scale[][] = {
                { "1", "2",   "3",  "4",  "5",  "6",  "7" },
                { "1", "1/2", "1/3","1/4","1/5","1/6","1/7" }
            };
            //LISTENER
            comboBox.addActionListener(
                    new ActionListener(){
                        @Override
                        public void actionPerformed(ActionEvent e){
                            JComboBox combo = (JComboBox)e.getSource();
                            //LOOK FOR THE RECIPROCAL ON THE SCALE
                            String item = (String)combo.getSelectedItem();
                            String itemReciprocal = "";
                            for (int i = 0; i <= 0; i++) {
                                for (int j = 0; j < 7; j++) {
                                    if(item.equalsIgnoreCase(scale[i][j])){
                                        itemReciprocal = scale[1][j];
                                    }                                                                         
                                }
                            }
                            //WITH THE RECIPROCAL I PROCEEDE TO SET THE CORRESPONDING CELL
                            int row = jTableCriteria.getSelectedRow()+1;
                            int column = jTableCriteria.getSelectedColumn();
                            int reciprocalRow = column ;
                            int reciprocalColumn = row ;
                            jTableCriteria.getModel().setValueAt(itemReciprocal, reciprocalRow , reciprocalColumn );

                        }
                    }            
            );
            weighting.setCellEditor(new DefaultCellEditor(comboBox));
        }

    }             
}

Upvotes: 0

mKorbel
mKorbel

Reputation: 109823

  • use code example from official Oracle tutorial How to use Table - Using a Combo Box as an Editor, in model is stoere only selected value from JComboBox, not JComboBox

  • you have to override setValueAt, 1st part inside setValueAt is about to store integer value from current JComboBox as CellEditor to XxxTableModel, second part is about to set value to (another JTables cell) another cell in XxxTableModel


  • I'd be

    1. to use DefaultTableModel

    2. there is to override getColumnClass for JComboBox (to have to contains integer to avoiding parsing)

    3. isCellEditable

    4. setValueAt in SSCCE form

Upvotes: 4

crzbt
crzbt

Reputation: 183

You can make ActionListener that listen to ComboBox state changed and change other ComboBox value. Also you can try PopupMenuEvent for this.

Upvotes: 0

Related Questions