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());
1
  • 1
    for better help sooner post an SSCCE/MCVE short runnable, compilable with hardcoded value for JTable/JComboBox in local variable Commented Aug 27, 2014 at 10:45

3 Answers 3

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()); 
Sign up to request clarification or add additional context in comments.

7 Comments

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.
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)
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, 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)
@Logar This is a compiler bug fixed in 1.8. bugs.openjdk.java.net/browse/JDK-8046017
|
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

1 Comment

this is should be the answer, as he put ComboBox inside the cell.
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.