4

I have a problem with downloading the number from JTable. In Eclipse I have jre JavaSE 1.7 and it is all ok. I opened my project in IntelliJ IDEA and chose 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

Error:

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

Edit:

New problem: The JTable I have 2 fields, ID and field2 (combobox) after selecting the value from the combobox wants to retrieve a value from the ID field so that they know which row I need to update.

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++;
                }
            }
        });

And error:

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)
    [...]

Error points to this line:

int rowid = Integer.getInteger(itemTable.getValueAt(
                                itemTable.getSelectedRow(), 0).toString());
lukassz
  • 127
  • 1
  • 2
  • 12
  • 1
    for better help sooner post an SSCCE/MCVE short runnable, compilable with hardcoded value for JTable/JComboBox in local variable – mKorbel Aug 27 '14 at 10:45

3 Answers3

15

Well look at the error:

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

And then look at the line that throws the error:

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

Now as you can see, you're attempting to cast an Object to an int. This is not allowed. So you need to be a little more creative:

int id = Integer.parseInt(table.getValueAt(table.getSelectedRow(), 0).toString()); 
christopher
  • 26,815
  • 5
  • 55
  • 89
  • I think OP wants to know why his code does compile under 1.7 compiler and not under 1.8 compiler. Good answer though, I personally wonder how the original code does compile with 1.7. – Logar Aug 27 '14 at 10:21
  • really not the solution, solving just the actual OPs issue, proper solution is to store integer value in model and/or to override getColumnClass, more in Oracle tutorial - How to use Tables, for working code example(s) – mKorbel Aug 27 '14 at 10:22
  • Correct, but I'm not here to do OP's work. I'm here to solve the issue that OP has presented. OP will develop a much deeper understanding if they come to them on their own. – christopher Aug 27 '14 at 10:24
  • @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)` – lukassz Aug 27 '14 at 10:28
  • Post your code change because that's not the error I'd expect from this code. – christopher Aug 27 '14 at 10:31
  • @lukassz this answer is wrong, as is mentoned by answerer in his comment to me, he don't want to solve anything, put Integer value to JComboBoxDefaultComboBoxModel, again don't to parse whatever in Java, Java is designated to nest all data types thought all Java APIs (excluding a few bugs) correctly – mKorbel Aug 27 '14 at 10:32
  • 1
    @Logar This is a compiler bug fixed in 1.8. https://bugs.openjdk.java.net/browse/JDK-8046017 – Jiayun Zhou Jul 14 '15 at 02:26
4

I have a problem with downloading the number from JTable.


edit

re:

@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)

  • don't put JComboBox to JTable, read Oracle tutorial How to use Tables - Using a Combo Box as an Editor for working code example (String instance), model should be store only initial or last selected value from JComboBox as Editor

  • put numbers to JComboBox/DefaultComboBoxModel directly, then returns is number

  • TableModelListener firing an event after CellEditor() == null, then code doesn't make me sence

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

See class Jtable

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

Method has return type as Object.

use Integer.parseInt();

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116