Java Quiz Player

Todos in a Java Swing JTable

May 10, 2021

1. Overview

This is an example Java GUI application. The application maintains a set of todos in a table. There are functions to create a new todo, save an updated todo, and delete a todo. You can also print all todos to the console and store the todos in a file.

The completed application's GUI looks like this:

GUI image

The GUI for this application is built using the Java Swing and Awt classes. The table where the todos are displayed is a JTable; it has two columns, the todo's name (or description text) and the done (or is completed, a boolean field) rendered as a JCheckBox.

The table data is managed using a table model. The model manages updates in the table UI after CRUD (Create, Read, Update and Delete) operations on the table data. For example, after a todo is created the model is updated, which in turn is reflected in the table UI. Other functions include the rendering of the check box in the table UI and maintaining the todo data shown in the table.

The table model is an instance of javax.swing.table.AbstractTableModel, an abstract class; this is extended to create the class customized for this application, TodoTableModel.java. Other classes in the application are:

The code is written using Java SE 8. The Java source code for the application can be browsed or downloaded from the Browse or Download section below.

2. Components in the GUI

The table is built using the JTable and is enclosed in a JScrollerPane for vertical scrolling of the table rows. There is a JTextField where the selected todo’s name is displayed, and can be edited (to be saved). There are buttons (JButton component) to create a new todo, save a modified todo's name, delete a todo, print the todos to the console and store todos to a file. There is a status message displayed at the bottom of the window; this is a JLabel.

These components are placed within their respective JPanel containers, which in turn are placed within the main window of the application, a JFrame.

There are various attributes set for components: fonts, component size, spacing within and between the components, etc., to get the desired look and feel.

3. GUI Layout

There is a main panel and individual containers for each or set of components. The containers are JPanels except for the table.

The main panel, has a BoxLayout with Y_AXIS and specifies the components are to be laid out vertically. This allows all the components - table, text field, buttons (together) and the message - are placed in a vertical stack. The main panel in turn is placed within the JFrame. Layouts allow place the components within the application window as required.

mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

The JTable is within a scrolling enabled container, the JScrollPane. Then, the text field is placed within its own panel. The buttons are placed in a panel with FlowLayout which allows buttons to be placed one after the other horizontally. The alignment is set as CENTER so that the panel is centred horizontally within the main panel.

buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

Finally, the message JLabel is in its own panel.

To create spacing in between, the components are separated with borders. Borders can be of different types - an empty border with space size is used to specify the space (or gap) between components. For example, the main panel has an empty border of size 10. This creates an empty space on all sides between the components and the main window border.

mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));

4. Application Functions

The todos in this application are stored in a text file.

At the start of the application all the todos from the file are read and displayed in the table. After that, the user can perform any actions mostly by selecting the table row (a todo) and clicking the buttons for respective functionality.

The button click and the table row selection have listeners which perform some logic based upon the action on the component; for example when a row (a todo) is selected in the table, the todo's name is displayed in the text field. Or, when the delete button is pressed, the selected todo in the table is deleted.

In addition to the GUI component listeners, there is a listener for the table model; this is triggered when there are changes in the table model due to new, updated or deleted data.

5. Browse or Download

Browse the Java source code online or download to your machine. You can compile and run the code - requires Java SE 8 or higher.

5.1. Browse

5.2. Download

todos-in-java-swing-jtable-example.zip

Return to top