Creating a JTable Component

In this Java programming tutorial we are going to implement the simple JTable on the java swing frame with the help of some java methods.

Creating a JTable Component

In this Java programming tutorial we are going to implement the simple JTable on the java swing frame with the help of some java methods.

Creating a JTable Component

Creating a JTable Component

     

Now you can easily create a JTable component. Here, the procedure for creating a JTable component is given with the brief description of JTable and it's components.

JTable: The JTabel component is more flexible Java Swing component that allows the user to store, show and edit the data in tabular format. It is a user-interface component that represents the data of  two-dimensional tabular format. The Java swing implements tables by using the JTable class and a subclass of JComponent

For learning complete JTable component you have to learn following parts:

  • JTable
  • Rendering
  • Table model
  • Observable event model
  • Tool tips

Rendering: In computer program, the rendering is a process that generates an image from a model. Model is a specification of three dimensional objects.

Table model: All tables contains of  its data from an object that implements the TableModel interface of java swing package. The TableModel interface uses the methods of JTable class that integrates a tabular data model.

Observable event model: The event model is a building block of graphical user interfaces (GUI). This is a user friendly and most common that is provided by the graphical frameworks. The observable is a system state property that determines by some sequence of physical operations.

Tool tips: The tool tips are most common graphical user interface that is used for displaying the message in small box given by the user appropriate message, when the mouse pointer or cursor goes on it, a message (description) appears without clicking it.

Description of program:

In this Java programming tutorial we are going to implement the simple JTable on the java swing frame with the help of some java methods. To create a JTable component, you need a java swing frame. The JTable based on the frame's panel contains multiple data in a tabular format such as rows and columns. 

Description of code:

JTable( Object data[][], Object col[]):
This is the constructor of JTable class that implements a JTable for displaying the values in tabular format like rows and columns. All rows must have the same length as column names. It takes two arguments: data and col.

data - It defines the all data that will have to create a JTable.
col - It specifies the name of each column of the JTable.

Here is the code of program:

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

public class JTableComponent{
  public static void main(String[] args
{

  new JTableComponent();
  }

  public JTableComponent(){
  JFrame frame = new JFrame("Creating 
JTable Component Example!"
);
  JPanel panel = new JPanel();
  String data[][] {{"vinod","BCA","A"},{"Raju","MCA","b"},
   {
"Ranjan","MBA","c"},{"Rinku","BCA","d"}};

    String col[] {"Name","Course","Grade"};
  JTable table = new JTable(data,col);
  panel.add(table,BorderLayout.CENTER);
  
frame.add(panel);
  frame.setSize(300,200);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Download this example.

Output of program: