JComboBox - 无法获得价值

时间:2013-08-01 02:39:29

标签: swing actionlistener jcombobox tablecelleditor

我在下表中创建了Jcombobox。

代码

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

当我尝试获取dropwon值时出错。

错误

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

我甚至尝试过这样的事。

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

但是我得到了类似的错误

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

请帮忙。感谢

1 个答案:

答案 0 :(得分:0)

更改此

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

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

您正在定义一个匿名类。为了避免使用java变量中的闭包产生的奇怪副作用,必须将其标记为final。