• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

To extract data from AbstractTableModel?

Hi,

I have a JTable based on an AbstractTableModel like this:

private myTableModel mtm = new myTableModel( );

jTable1.setModel(mtm);

(see code below)

my users can edit records in the table and then I want to simply do some math on the floats that they type.

How should I get these numbers out of the tableModels data array?  Do I use getValueAt( )?  But that returns an Object?

Thanks,

DevonportA1.


public class myTableModel extends AbstractTableModel {
   
   
    public myTableModel() {
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }
   
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
   
    public boolean isCellEditable(int row, int col) {
            if (col == 0)
                    return false;
            else return true;                    
    }
   
   
   public void setValueAt(Object value, int row, int col) {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }

   
    public String getColumnName(int col){
        return columnNames[col];
    }
   
    private String[] columnNames = {"Col1",
                                    "Col2",
                                    "Col3",
                                    "Col4",
                                    "Col5",
                                    "Col6",
                                    "Col7"};
                                                               
                       
   private Object[][] data = {
    {"Field 1",new String(""), new Float(0.0),new Boolean(false), new Boolean(false), new Boolean(false),new Float(0.0)},
    {"Field 2",new String(""), new Float(0.0),new Boolean(false), new Boolean(false), new Boolean(false),new Float(0.0)},
    {"Field 3",new String(""), new Float(0.0),new Boolean(false), new Boolean(false), new Boolean(false),new Float(0.0)},
    {"Field 4",new String(""), new Float(0.0),new Boolean(false), new Boolean(false), new Boolean(false),new Float(0.0)},
    {"Field 5",new String(""), new Float(0.0),new Boolean(false), new Boolean(false), new Boolean(false),new Float(0.0)},
    {"Field 6",new String(""), new Float(0.0),new Boolean(false), new Boolean(false), new Boolean(false),new Float(0.0)},
    {"Field 7",new String(""), new Float(0.0),new Boolean(false), new Boolean(false), new Boolean(false),new Float(0.0)}};    
   
}




0
DevonportA1
Asked:
DevonportA1
1 Solution
 
zzynxSoftware engineerCommented:
>> Do I use getValueAt( )?  
Yes.
>> But that returns an Object?
What's the problem with that?
0
 
objectsCommented:
Float fl = (Float) model.getValueAt(col, row);
float f = fl.floatValue();
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now