Sei sulla pagina 1di 5

JTable Component

JTable
The JTable is used to display and edit
regular two-dimensional tables of cells.
With the JTable class you can display tables
of data, optionally allowing the user to edit
the data. JTable does not contain or cache
data; it is simply a view of your data.
To add JTable Component
Go to Palette window, under the Swing
Controls click the JTable component
Click on your JFrame to add the table
You may customize the Header and the
number of columns by accessing the
model property.
Adding Content in your Table

Import DefaultTableModel that will represent the


model of the table
import javax.swing.table.DefaultTableModel;
Declare the model object inside you JFrame class
DefaultTableModel model;
To add data in your table
model=(DefaultTableModel)tblstudent.getModel();
//tblstudent is the name of JTable component
//sets the model object as the model of the table
model.insertRow(model.getRowCount(),new Object
[] {value1,value2,value3,});
model.insertRow(model.getRowCount(),new
Object [] {value1,value2,value3,});

The First argument of the insertRow


method , model.getRowCount, sets the row
number where the data will be inserted in
the table
The Second argument creates an array
objects where the elements represents the
data for each column. The values
represents the data for the table. It can be
constant value, variable or your data from
your database.

Potrebbero piacerti anche