How to Create a Table in Java?17 Mar 2025 | 6 min read Creating a table in Java typically involves using data structures like arrays or collections. There are a few different ways to create a table in Java. One way is to use the JTable class. In this section, we will discuss the various ways to create table in Java. 1. Using a 2D ArrayTableCreationGUI.java Output: ![]() Explanation Swing is used by the TableCreationGUI class to provide a graphical user interface (GUI) by extending JFrame. The table in this GUI is represented by a JTable component, and the data is given by means of a 2D array. To make sure that the GUI is built on the Event Dispatch Thread (EDT), the programme makes use of the SwingUtilities.invokeLater() method. To act as the model for the JTable, the data in the 2D array is transformed into an object array (Object[][] dataArray). To enable scrolling in the event that the table content exceeds the frame size, a JScrollPane is also included. 2. Using ArrayList of ArrayList:TableCreationGUI.java Output: ![]() Explanation The programme represents the table's rows and columns using an ArrayList<ArrayList<Integer>>. Swing components are used to form the GUI, and the rows and values in the ArrayList of ArrayLists are iterated over using an Iterator. Next, the data is transformed into an object array for the JTable model (Object[][] dataArray). The application offers a dynamic and adaptable framework for making and presenting tables of different sizes. 3. Using Arrays and StringBuilder Class:TableCreationGUI.java Output: ![]() Explanation This program's GUI uses a JTextArea component to display a table inside a scrollable text area. Using nested while loops and a StringBuilder, the table's content is created. The StringBuilder is then used to create the table's string representation. If the table's content is larger than the frame, a JScrollPane is added to enable scrolling. This method offers a straightforward approach to display tables in a graphical user interface text field. 4. Using Java Collections Framework (List of Lists):TableCreationGUI.java Output: ![]() Explanation The rows and columns of the table are represented by this programme using a List<List<Integer>> and the Java Collections Framework. Swing components are used to create the GUI, and an Iterator is used to iterate through the values and rows in the List of Lists. For the JTable paradigm, the data is transformed into an object array (Object[][] dataArray), much like in earlier examples. This method offers readability and versatility when making and presenting tables. 5. Using Java StreamsTableCreationGUI.java Output: ![]() Explanation Java Streams are used in this programme to expedite the development and printing of the table. Swing components are utilised to generate the GUI, and nested while loops are used to fill the StringBuilder with table information. Next, a scrollable text box with a JTextArea containing the generated string is shown. Using Java Streams makes code more expressive and concise, which improves readability and maintainability. 6. Using String Formatting for a Neat DisplayTableCreationGUI.java Output: ![]() Explanation This program's GUI makes use of String.format() to produce a tidy table display. As in earlier examples, nested while loops are used to create the table content, and the resultant string is shown in a scrollable text area contained in a JTextArea. Next TopicID Card Generator Using Java |
The Josephus Problem is a theoretical problem related to a certain elimination game. It is called after the Jewish historian Flavius Josephus, who, as per legend, created this technique to evade capture during a siege. Problem Statement n people are standing in a circle,...
10 min read
Similar to primitive types, Java makes it easier to give objects as parameters to methods. It is crucial to recognize that sending an object as an argument transmits merely a reference to the item-not a duplicate of it. It means that any changes made to the...
5 min read
The sort() method that is a part of Comparator mechanism of Collection class that sorts the data in decreasing order. If we want to achieve the aim in generic while considering the boundary condition where the objects to be sorted are user-defined, we can use Comparator interface....
3 min read
A string is given that is made only of digits, such that the string represents a number. Our task is to split the number string in such a way that each segment of the number that is formed after the split is a prime number. Also,...
10 min read
Swapping of rows to arrange a binary grid with minimum swaps is an exciting problem that requires the conversion of the given binary grid to a particular form. The objective is to make sure that for every row i in the grid, it has atleast...
31 min read
Working with PDF files often involves creating, modifying, and formatting for specific needs. Tiling divides a single page's content into smaller portions and redistributes them across multiple pages, useful for printing, posters, or improving readability. It covers developing a Java program to tile PDF content using...
5 min read
In this article, we will be acknowledged about singleton Enum in Java. You will be able to understand how to use it, its purposes and most importantly its advantages and disadvantages. Let's understand what is a singleton in programming. Singleton A class called a singleton is one that is...
4 min read
In this section, we will learn what is sunny numbers and how to create a Java program to find the sunny numbers. We will also create a Java program to find all the sunny numbers between the specified range. Sunny Number A number is called a sunny number...
3 min read
A Java API called Java Database Connectivity (JDBC) enables Java programs to access and work with data stored in databases. JDBC offers a common set of classes and interfaces to connect to databases, run SQL commands, and retrieve data. The software elements known as JDBC drivers...
4 min read
In Java especially in applications, managing time as well as date is one of those tasks that is very common. JDK 8 contains time package that includes the set of classes for working with the time and date of these, the LocalTime class is particularly created...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India