Java Tutorial : How to Use Swing JTables

The Swing java package is used to create GUI for user inputted data. It is a really useful library that allows you to easily create applications to handle data. Some functions include JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu…etc. However this tutorial will cover JTables.

 JTable are used to display and edit regular two-dimensional tables of cells.

This tutorial will show how to create a Jtable, input data into the table and create a button that will print out values of a column.

Here the columns are given Titles and the data is being set for the JTable. In this example, the data that is being inputted is a Land Lords Tenant list. This List is used to keep track of renters who still need to pay their rent. In this table there is the renter’s Tenant #, Name ,Rent Amount and Paid/Unpaid status.

 //Table Headers
        String[] columns = new String[] {
            "Tenant # ", "Name", "Rent Amount", "Paid/Unpaid"
        };
         
        //data for array 
        Object[][] data = new Object[][] {
            {1, "John", 600 , "Paid" },
            {2, "Jane", 1000, "Paid" },
            {3, "Mat", 725, "Unpaid" },
            {4, "Kelly", 600 , "Paid" },
            {5, "Pat", 1000, "Unpaid" },
            {6, "Kate", 725, "Unpaid" },
        };

This portion of code adds that data to the table as well as creates the button. the button will print out the last Column which shows is the tenant has paid their rent or not.

 //create table with data
        JTable table = new JTable(data, columns);
        JButton button = new JButton("Click Here..!");
                
        //add the table to the frame
        this.add(new JScrollPane(table));
        //add button
        this.add(button, BorderLayout.PAGE_END);

Controls what happens when the button is clicked

//action when button is clicked
        button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				ArrayList<Object> list = new ArrayList();
				for(int i = 3;i<table.getModel().getRowCount();i++)
				{
				list.add(table.getModel().getValueAt(i,3)); //get the all row values at column index 0
	
				//printing out list
				System.out.print(list);
				}
					

Final portion of code, this sets the title of the JFrame that hosts the JTable data

this.setTitle("Table Example");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        this.pack();
        this.setVisible(true);
    }
     
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Table();
            }
Output :

When the button at the top is click the values of the Paid/Unpaid column should be printed out in console

Full Code
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;

public class Table extends JFrame
{
    public Table()
    {
        //Table Headers
        String[] columns = new String[] {
            "Tenant # ", "Name", "Rent Amount", "Paid/Unpaid"
        };
         
        //data for array 
        Object[][] data = new Object[][] {
            {1, "John", 600 , "Paid" },
            {2, "Jane", 1000, "Paid" },
            {3, "Mat", 725, "Unpaid" },
            {4, "Kelly", 600 , "Paid" },
            {5, "Pat", 1000, "Unpaid" },
            {6, "Kate", 725, "Unpaid" },
        };
        //create table with data
        JTable table = new JTable(data, columns);
        JButton button = new JButton("Click Here..!");
                
        //add the table to the frame
        this.add(new JScrollPane(table));
        //add button
        this.add(button, BorderLayout.PAGE_START);
        
        
        //action when button is clicked
        button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				ArrayList<Object> list = new ArrayList();
				for(int i = 3;i<table.getModel().getRowCount();i++)
				{
				list.add(table.getModel().getValueAt(i,3)); //get the all row values at column index 0
	
				//printing out list
				System.out.print(list);
				}
					
			}
				 
        });
        
        this.setTitle("Table Example");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        this.pack();
        this.setVisible(true);
        
        
        
    }
     
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Table();
            }
        });
    }   
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s