2014-08-27 74 views
1

我從JTable下載數字時遇到問題。在Eclipse中,我擁有JavaSE 1.7並且它都可以。我在IntelliJ IDEA中打開了我的項目並選擇了SDK java jdk 1.8。無法將java.lang.Object轉換爲int

private int; 
public void tableEdit(final JTable table) { 

     table.getModel().addTableModelListener(new TableModelListener() { 
      @Override 
      public void tableChanged(TableModelEvent e) { 
       // TODO Auto-generated method stub 
       if (table.getCellEditor() != null) { 

        int col = table.getSelectedColumn(); 
        id = (int)table.getValueAt(table.getSelectedRow(), 0); //ERROR 

錯誤:

java: incompatible types: java.lang.Object cannot be converted to int 

編輯:

新的問題: JTable中我有2場,ID並從下拉框選擇值後場2(組合框)想要檢索來自ID字段的值,以便他們知道我需要更新哪一行。

categoryBox.addItemListener(new ItemListener() { 
      public void itemStateChanged(ItemEvent event) { 
       if (newrow_flag == 0) { 
        JComboBox comboBox = (JComboBox) event.getSource(); 
        Object item = event.getItem(); 
        if (event.getStateChange() == ItemEvent.SELECTED 
          && box_flag_category > 0) { 

         Category selected_category = (Category) categoryBox 
           .getSelectedItem(); 

         int rowid = Integer.getInteger(itemTable.getValueAt(
           itemTable.getSelectedRow(), 0).toString()); //Error 

         id_category = selected_category.getId(); 


         fireItemEvent(new ItemsEvent(rowid, "produkty", null, 
           null, null, id_category, id_company, "update"), 
           "box_category"); 

        } 
        box_flag_category++; 
       } 
      } 
     }); 

和錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at com.magazyn.view.View$9.itemStateChanged(View.java:659) 
    at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1223) 
    at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1280) 
    at javax.swing.JComboBox.contentsChanged(JComboBox.java:1327) 
    at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118) 
    at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93) 
    at javax.swing.JComboBox.setSelectedItem(JComboBox.java:576) 
    at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:622) 
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:834) 
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290) 
    at java.awt.Component.processMouseEvent(Component.java:6527) 
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) 
    [...] 

錯誤指向該行:

int rowid = Integer.getInteger(itemTable.getValueAt(
           itemTable.getSelectedRow(), 0).toString()); 
+1

爲更好地幫助儘快發佈一個SSCCE/MCVE可短期運行,可編譯JTable/JComboBox的硬編碼值在局部變量中 – mKorbel 2014-08-27 10:45:17

回答

8

遠遠看錯誤:

java: incompatible types: java.lang.Object cannot be converted to int

再看看行拋出錯誤:

id = (int)table.getValueAt(table.getSelectedRow(), 0); 

現在,您可以看到,您正試圖將Object投射到int。這是不允許的。所以,你需要多一點創意:

int id = Integer.parseInt(table.getValueAt(table.getSelectedRow(), 0).toString()); 
+0

我想OP想知道他的代碼爲什麼在1.7編譯器下編譯而不是在1.8編譯器下編譯。很好的答案,但我個人不知道原始代碼是如何用1.7編譯的。 – Logar 2014-08-27 10:21:38

+0

真的不是解決方案,只解決實際的OP問題,正確的解決方案是將整數值存儲在模型中和/或覆蓋getColumnClass,更多信息請參閱Oracle教程 - 如何使用表格以獲取工作代碼示例 – mKorbel 2014-08-27 10:22:42

+0

正確,但我不是來做OP的工作。我在這裏解決OP提出的問題。如果他們自己找到OP,他們會得到更深的理解。 – christopher 2014-08-27 10:24:02

0

見類JTable中

public Object getValueAt(int row, int column) { 
     return getModel().getValueAt(convertRowIndexToModel(row), 
            convertColumnIndexToModel(column)); 
    } 

方法返回類型爲對象

使用Integer.parseInt();

4

I have a problem with downloading the number from JTable.


編輯

重:

@christopher, when I chose value from ComboBox i get error Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at com.magazyn.view.View$9.itemStateChanged(View.java:659) at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1223) at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1280)

  • 不要把JComboBox中到JTable中,閱讀甲骨文教程How to use Tables - Using a Combo Box as an Editor的工作代碼示例(String實例),模型應僅存儲JComboBox中的初始或最後選定值作爲編輯器

  • 放號碼的JComboBox/DefaultComboBoxModel直接,然後返回編號爲

  • TableModelListener發射CellEditor的()== NULL後一個事件,然後代碼並不讓我SENCE

+0

這應該是答案,因爲他將ComboBox放入單元格中。 – 2014-08-27 20:45:01