【问题标题】:JComboBox - Unable to get the valueJComboBox - 无法获取值
【发布时间】:2013-08-01 02:39:29
【问题描述】:

我已经在我的表中创建了 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

请帮忙。谢谢

【问题讨论】:

  • 你必须声明它是最终的..
  • ehhh ...您正在收听 renderer 的组合以访问 editor 的值?两者都是错误 ...就像非常错误
  • @kleopatra 我可以将其更改为监听编辑组件并访问渲染组件吗? ——
  • nooo - 它们不适用于外部(== JTable 内部之外)使用。退后一步,解释一下你想要实现的什么
  • @kleopatra 我只想在我的 Jtable 中创建一个组合框。我使用了代码并且工作正常。我只是无法获得选定的值,一旦我按照下面的答案输入最终关键字,它就修复了。

标签: swing actionlistener jcombobox tablecelleditor


【解决方案1】:

改一下

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

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

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

【讨论】:

  • 如果我将 TableColumn col5 更改为 final。我无法获取选定的下拉列表值。
  • 没有错误。由于您将 col5 声明为最终结果。它采用下拉默认值。由于 col5 已经是最终的,我无法在操作侦听器中获取 drop selected 值
  • @chinna_82 :S final 意味着它不会指向另一件事,但可以变异...尝试定义final combo和内部动作监听器combo1.getSelectedItem()
猜你喜欢
  • 1970-01-01
  • 2012-10-08
  • 2012-04-21
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
  • 2015-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多