Getting and Setting a Cell Value in a JTable Component - Java Swing

Java examples for Swing:JTable Cell

Description

Getting and Setting a Cell Value in a JTable Component

Demo Code

import javax.swing.JTable;


public class Main {

  public static void main(String[] argv) {
    // Retrieve the value in the visible cell (1,2)
    int rowIndex = 1;
    int vColIndex = 2;
    JTable table = null;/*from w  w  w  . j a v a 2 s  .  c  om*/
    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 Tutorials