java.lang.Object无法转换为int

时间:2014-08-27 10:16:34

标签: java swing jtable

从JTable下载号码时遇到问题。在Eclipse中我有jre JavaSE 1.7,一切都好。我在IntelliJ IDEA中打开了我的项目,并选择了SDK java jdk 1.8。

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和field2(组合框),从组合框中选择值后,想要从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());

3 个答案:

答案 0 :(得分:10)

看看错误:

  

java:不兼容的类型:java.lang.Object无法转换为int

然后查看抛出错误的行:

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

现在您可以看到,您正在尝试将Object投射到int。这是不允许的。所以你需要更有创意:

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

答案 1 :(得分:4)

  

从JTable下载号码时遇到问题。


修改

重新:

  

@christopher,当我从ComboBox中选择值时,我得到错误异常   在线程" AWT-EventQueue-0" java.lang.ClassCastException:   java.lang.Integer无法强制转换为java.lang.String   com.magazyn.view.View $ 9.itemStateChanged(View.java:659)at   javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1223)at at   javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1280)

  • 不要将JComboBox放到JTable中,阅读Oracle教程How to use Tables - Using a Combo Box as an Editor以获取工作代码示例(String instance),模型应该只存储来自JComboBox的初始或最后选择的值作为编辑器

  • 直接将数字放到JComboBox / DefaultComboBoxModel,然后返回数字

  • TableModelListener在CellEditor()== null之后触发一个事件,然后代码没有让我看到

答案 2 :(得分:0)

参见课程 Jtable

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

方法的返回类型为对象

使用Integer.parseInt();