2013-08-01 88 views
-1

我已經按照以下方式在我的表格中創建了Jcombobox。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(); 

,但我得到了similiar錯誤

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

請幫助。由於

+0

您必須聲明最終的結果.. – nachokk

+0

ehhh ...您正在傾聽_renderer_的組合以訪問_editor_的值嗎?兩者都是_wrong_ ...因爲**非常錯誤** – kleopatra

+0

@kleopatra我可以將其更改爲偵聽編輯組件並訪問渲染組件嗎? - –

回答

0

更改此

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

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

您正在定義一個匿名類。爲了避免在java變量中使用閉包的奇怪副作用,必須將其標記爲final。

+0

如果我將TableColumn col5更改爲final。我無法獲得所選的下拉列表值。 –

+0

爲什麼,你有什麼錯誤? – nachokk

+0

沒有錯誤。既然你聲明col5爲final。它採取下拉默認值。我不能得到下降選擇值在行動listernersince col5已經是最終 –