Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Making a JTable in swing using Java

In this article you will see how create a JTable. This table can be used to populate the data been extracted from the database and then showing it in an interactive manner.

This can be helpful in situation where you have a lots of data and you want to show that data in a proper way on the screen. Lets start with the explanation. First we will import all the required classes.

Listing 1: Importing classes

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

Here,

  1. FlowLayout is used to define the layout of the frame to be shown on the screen.
  2. JFrame is used to show the frame on the screen.
  3. JScrollPane is used to implement a scrollbar on the table which can be helpful in situation where there is lots of data to be shown on the table and will require scrolling to show complete data on single page
  4. Jtable is used to implement a table on the frame
  5. DefaultTableModel is the default table model and is used to define the structure of table

Now we will make the class and make the main method:

Listing 2: Making class and defining the main method

public class JTableUsage extends JFrame
{
	DefaultTableModel model;
	JTable table;
	String col[] = {"Name","Address","Phone"};
public static void main(String args[])
{
new JTableUsage().start();	
}

 

Here,

  1. The name of class is JTableUsage and it extends JFrame because we want to implement a frame
  2. We define variables model, table, col. Here col will contain all the column of the table to be shown on the table
  3. We define a main method which call the start method of this class. This start method will contain all the logics

Now we will define the start method which will contain the main processing

Listing 3: Defining the start method

public void start()
{
	
	 model = new DefaultTableModel(col,50);	
		table=new JTable(model){@Override
		public boolean isCellEditable(int arg0, int arg1) {
		
			return false;
		}};
	JScrollPane	pane = new JScrollPane(table);
	
	table.setValueAt("csanuragjain",0,0);
	
	add(pane);
	setVisible(true);
	setSize(500,400);
	setLayout(new FlowLayout());
	setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
 

Here,

  1. We made an object called model and pass it the columns which we want to show in our table and 50 represents the number of rows to be displayed on the table
  2. Now we define a table variable and pass the model variable to this so that table gets the properties of the model.
  3. We override the method isCellEditable and put false as return value so that no value in the table is editable
  4. We define a variable pane which will help to implement a scroll bar in the table so that the user is capable of scrolling within the table
  5. We use the setValueAt method so that we can place any value at a particular row and column in the table. Here in the arguments first we pass the string to be displayed and then we put the row number and then the column number. Here both row and column number starts from zero
  6. Now we add this on the frame making use of add method and pass the pane variable to it
  7. We set the frame visibility to true and we also set the size of frame
  8. We set the layout of frame as FlowLayout which places the components in frame one after the other
  9. We set the default close operation as EXITONCLOSE so that the application dies when anyone clicks on the close button of the frame

Listing 4: Full Source code

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class JTableUsage extends JFrame
{
	DefaultTableModel model;
	JTable table;
	String col[] = {"Name","Address","Phone"};
public static void main(String args[])
{
new JTableUsage().start();	
}

public void start()
{
	
	 model = new DefaultTableModel(col,2);	
		table=new JTable(model){@Override
		public boolean isCellEditable(int arg0, int arg1) {
		
			return false;
		}};
	JScrollPane	pane = new JScrollPane(table);
	
	table.setValueAt("csanuragjain",0,0);
	
	add(pane);
	setVisible(true);
	setSize(500,400);
	setLayout(new FlowLayout());
	setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

Output

JTable using Swing

This is all. Hope you liked the article. See you next time.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login