Setting Cell Values in JTable

In this section, we will learn how to set the cell values in JTable component.

Setting Cell Values in JTable

In this section, we will learn how to set the cell values in JTable component.

Setting Cell Values in JTable

Setting Cell Values in JTable

     

In this section, we will learn how to set the cell values in JTable component. For this you must have the some previous knowledge about JTable. A cell is known as the format of  a row and a column in  JTable containing data in it. Each cell has it's own address where the data is stored. 

Description of program:

In this program, you will see the process of  setting the cell values in JTable. This program sets the data in the specified location in terms of cell address in the JTable. We have set the value in the 3rd rows and 3rd column in the below program.  This cell address has 'Math' value. Another cell address is 4 row and 1 column. The cell value of  that position is specified as  'Santosh'.  To set the cell values in the desired position in the JTable,  we created  the SetData method and set the cell value by applying  setValueAt() method on  the specified position in the JTable.

Description of code:

getModel():
This method returns the TableModel that displays the data of JTable.

setValueAt(Object obj, int row_index, int col_index):
This method is used to set the given value in a cell of  JTable at the specified row and column address. It takes the following parameters:

    obj: This is the new cell value that is used to add a data  in JTable.
  row_index: This is the index of row that is  to be changed.
  col_index: This is the index of column that is to be changed.

Here is the code of program:

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class SetCellValues{
  JTable table;
  public static void main(String[] args) {
  new SetCellValues();
  }

  public SetCellValues(){
  JFrame frame = new JFrame("Setting Cell Values in JTable");
  JPanel panel = new JPanel();
  String data[][] {{"Vinod","MCA","Computer"},
   {
"Deepak","PGDCA","History"},
   {
"Ranjan","M.SC.","Biology"},
   {
"Radha","BCA","Computer"}};
  String col[] {"Name","Course","Subject"};  
  DefaultTableModel model = new DefaultTableModel(data, col);
  table = new JTable(model);
  SetData("Math",2,2);
  SetData("Santosh",3,0);
  JTableHeader header = table.getTableHeader();
  header.setBackground(Color.yellow);
  JScrollPane pane = new JScrollPane(table);
  panel.add(pane);
  frame.add(panel);
  frame.setSize(500,150);
  frame.setUndecorated(true);
  frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public void SetData(Object obj, int row_index, int col_index){
  table.getModel().setValueAt(obj,row_index,col_index);
  System.out.println("Value is added");
  }
}

Download this example.

Output of program:

Before setting cell values:

After setting cell values: