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());
                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()); 
                        I have a problem with downloading the number from JTable.
put integer value directly to JTable/XxxTableModel, to avoid any parsing at runtime
JTable is designated to store various data types in its XxxTableModel
(for DefaultTableModel isn't required) then to override getColumnClass
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With