JTable in Java with Example

JTable in Java with Example

beginning with programming logo

Table of Contents

a) Introduction to JTable
b) Constructor of JTable
c) Example with Source Code
d) Output


JTable is a swing component. It is used to display data in the form of rows and columns. It helps to arrange the data in such manner which helps the user to interact with the program more easily. If a table is directly added to frame, column headings and scroll bar does not appear. Therefore, we need to use JScrollBarPane class in-order to work with JTable.

JTable has a one constructor i.e. JTable(Object [][]data, String columns). Its constructor takes two argument which is of Object type and String type.

Let us create the JTable;

Source Code:

 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.

 * @author AnkitPC
import javax.swing.*;
import java.awt.*;

public class JTableDemo {
    JFrame f;
    JScrollPane jsp;
    JTable t;
        f=new JFrame("Table Demo");
     //setting size
        //setting layout
        f.setLayout(new BorderLayout());
        String []cols={"ID","Name","Salary"};
        t=new JTable(data,cols);
       // t.setEnabled(false);
      // t.setSelectionForeground(Color.yellow);
       // t.setGridColor(Color.BLUE);
        jsp=new JScrollPane(t);
     //adding the component
    public static void main(String[] args) {
        //calling the constructor
        new JTableDemo();


jtable in java

The above output shows the JTable.


Previous Lessons;

Post a Comment