Question

I've created Jcombobox in my table as per below.

Code

          TableColumn col5 = jTable1.getColumnModel().getColumn(4);      
          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));
          col5.setPreferredWidth(150);
          combo2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {                
                String dropDownValue = col5.getCellEditor().getCellEditorValue().toString();
                if(dropDownValue.equalsIgnoreCase("others"))
                {
                    JOptionPane.showMessageDialog(null, "alert", "alert", "");
                }
            }
        });

There is an error when I try to get the dropwon value.

Error

local variable col5 is accessed from within inner class; needs to be declared final.

I even tried like this.

String dropDownValue = combo1.getSelectedItem().toString();

but i get the similiar error

local variable combo1 is accessed from within inner class; needs to be declared final

Please help. Thanks

Was it helpful?

Solution

Change this

 TableColumn col5 = jTable1.getColumnModel().getColumn(4); 

to

 final TableColumn col5 = jTable1.getColumnModel().getColumn(4); 

You are defining an anonymous class. To avoid strange side-effects with closures in java variables must be marked as final.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top