How to Create a Table in Java?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 |
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