Mike Haye
Mike Haye

Reputation: 793

DefaultTableModel getValueAt method generating seemingly random results

I have a GUI with a JTable using the DefaultTableModel.

These instance variables are declared:

boolean doRun = false;
Class clazz;
Object obyect;
DefaultTableModel model;
ArrayList<String> al = new ArrayList();

The table is populated by this:

public StatusGUI(Object invokerObject) {
    initComponents();
    setLocationRelativeTo(null);
    clazz = invokerObject.getClass();
    obyect = invokerObject;
    String line;
    try {
        Field[] fields = clazz.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            if (!fields[i].isAccessible()) {
                fields[i].setAccessible(true);
            }
            if (("" + fields[i].getType()).equals("class java.lang.String")) {
                line = "String";
            } else {
                line = "" + fields[i].getType();
            }

            //Note: The first string in the Object is the description, which is left empty

            model.insertRow(0, new Object[]{"", fields[i].getName(), line, "" + fields[i]});
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    setVisible(true);
}

This generates (in this case) 5 rows containing information about variables.

I want to receive and store the information of these variables on button press with the following code:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    for (int i = 0; i < model.getRowCount(); i++) {
        String description = "" + model.getValueAt(i, 0);
        System.out.println("Description " + i + ": " + description);
        String name = "" + model.getValueAt(i, 3);
        if (!description.equals("") && description != null) {
            al.add(description + "::" + name);
        }
    }
    if (al.isEmpty()) {
        JOptionPane.showMessageDialog(this, "No descriptions were added to any of the variables."
                + "\nThis could also be because no variables were found - if so, please see 'Help'");
    } else {
        new Thread(new SendThread(al, obyect)).start();
        this.dispose();
    }
}                                        

When adding descriptions to all of the five rows, the above code generates the following output:

Description 0: d1
Description 1: d2
Description 2: d3
Description 3: d4
Description 4: 

When only adding a description in the first row of the JTable, the above code generates:

Description 0: 
Description 1: 
Description 2: 
Description 3: 
Description 4: 

This shows that it recognizes all five rows, but for some reasons messes up when reading from the rows.

I've been staring at the same lines of code for an hour now, and honestly don't see what's going wrong.

Thanks in advance, Mike.

Upvotes: 1

Views: 1547

Answers (2)

camickr
camickr

Reputation: 324098

However, why don't I seem to be able to just do this in the constructor?

You can set a property on the table to do this for you. See Table Stop Editing.

Upvotes: 3

Walter Laan
Walter Laan

Reputation: 2976

My crystal ball says your table is still editing (line 5 in first example, line 0 in the second). Commit that edit first in the action performed.

if (table.isEditing()) {
    table.getCellEditor().stopCellEditing();
}

Upvotes: 3

Related Questions