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:

|
Current Comments
1 comments so far (post your own) View All Comments Latest 10 Comments:Do me favour....?, I'm Newbie
i've been frustated about JTable,i've problem to querying record onto JTable.The Case is:
1. i wanna querying records into JTable but i want to search the record when i type into cell and the record result into JTable.
2. i wanna records When the program running in the blank position
examples :
i wanna search The Name of James and all about james result into JTable
position :
=================================================
|Names | Nick Names |Address | Gender | Phone |
=================================================
..... | | | | |
=================================================
^
|
Type here and the records results into JTable, results :
=================================================
|Names | Nick Names |Address | Gender | Phone |
=================================================
James |Bond |Lake City| Male |12345 |
=================================================
can you help me,please give me the sample source code and send into my email address,cos i'm newbie and start to begins java program.Thank Before and Thanks a lot.
Posted by Fajar Arianto on Tuesday, 04.1.08 @ 22:17pm | #55053