[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • 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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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