Making Simple JTable

The JTable is almost great component of Java Swing which is used to display records of any data.
Specially JTable are used for displaying records which are retrived from the database like MySQL, ORACLE ,Excel and other…

Simply JTable has it’s default model for displaying the records in format.
In this blog we would just demonstrate a simple JTable where all the data records are being assigned by developers.

Ok, here is a detail codes of Making simple JTable:

[sourcecode=’java’]
/*
* A simple program for displaying records
* in table format by using JTable Class
* of java swing Component
*
*
* Two Dimentional array is used to show data and columns
* or you can also use Vector for the datatype of
* table values and columns
*/

/**
*
* @author Narayan
*/

//import directives
//——————-
import javax.swing.*;
//——————-

public class JTableTest {

//————————–
//ATTRIBUTES
//————————–
private static JFrame frame;
private static JTable table;
private static String[][] data;
private static String column[];
private static JScrollPane scroll;

//**********************
//CONSTRUCTOR
//**********************
public JTableTest(){
//making object JFrame
frame = new JFrame(“SIMPLE JTABLE”);

column = new String[5];
//assigning values of column
column[0] = “Col1”;
column[1] = “Col2”;
column[2] = “Col3”;
column[3] = “Col4”;
column[4] = “Col5”;

//>>>>>>>>>>>>>>Two Dimensional Array <<<<<<<<<<<<<<<<<<<<<<<<<<< //declaring the Object as String[][] with it's capability data = new String[10][5]; /* Looping for the assigning value of data*/ for(int i = 0; i< 10; i++){ for(int j = 0; j<5; j++) data[i][j] = "Record: Row:"+(i+1) +"Column: " +(j+1); } table = new JTable(data,column); table.setFillsViewportHeight(true); //For the scroll of the table records and Column visible scroll = new JScrollPane(table); //AT last adding at container of JFrame frame.getContentPane().add(scroll); frame.setVisible(true); //show frame.setSize(900,300); //Frame size } public static void main(String[] args){ //Compile and Excute constructor new JTableTest(); } } [/sourcecode] The output of the following codes shows like this: [caption id="attachment_645" align="aligncenter" width="300" caption="JTable"][/caption]

Leave a Reply