The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.table  [62 examples] > Cells  [5 examples]

e926. Getting and Setting a Cell Value in a JTable Component

    // Retrieve the value in the visible cell (1,2)
    int rowIndex = 1;
    int vColIndex = 2;
    Object o = table.getValueAt(rowIndex, vColIndex);
    
    // Retrieve the value in cell (1,2) from the model
    rowIndex = 1;
    int mColIndex = 2;
    o = table.getModel().getValueAt(rowIndex, mColIndex);
    
    // Change a cell in the 2nd visible column
    rowIndex = 2;
    vColIndex = 1;
    table.setValueAt("New Value", rowIndex, vColIndex);
    
    // Change a cell in the 3rd column in the model
    rowIndex = 3;
    mColIndex = 2;
    table.getModel().setValueAt("New Value", rowIndex, mColIndex);

 Related Examples
e927. Using Built-In Cell Renderers and Editors in a JTable Component
e928. Creating a Custom Cell Renderer in a JTable Component
e929. Creating a Class-Based Custom Cell Renderer in a JTable Component
e930. Copying Cell Values to the Clipboard from a JTable Component

See also: Column Heads    Columns    Editing    Events    Layout    Rows    Scrolling    Selection    Sorting    Table Model    Tool Tips   


© 2002 Addison-Wesley.