- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create JTable from two dimensional array in Java?
With two dimensional array, set the columns of a table. Additionally, we have set the rows using a one-dimensional array as shown below −
DefaultTableModel tableModel = new DefaultTableModel(new Object[][] { { "Mobile Phones", "100" }, { "RAM", "200" }, { "Caps", "50" }, { "Tablet", "80" }, { "LED", "400" }, { "Trousers", "350" }, { "T-Shirt", "500" }, { "Hoodie", "650" }, { "Jeans", "900" } }, new Object[] { "Items", "Quantity" } );
Now set the table model to the table −
JTable table = new JTable(tableModel);
The following is an example to create a table from two dimensional array −
Example
package my; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo { public static void main(String[] argv) throws Exception { DefaultTableModel tableModel = new DefaultTableModel(new Object[][] { { "Mobile Phones", "100" }, { "RAM", "200" }, { "Caps", "50" }, { "Tablet", "80" }, { "LED", "400" }, { "Trousers", "350" }, { "T-Shirt", "500" }, { "Hoodie", "650" }, { "Jeans", "900" } }, new Object[] { "Items", "Quantity" } ); JTable table = new JTable(tableModel); Font font = new Font("Verdana", Font.PLAIN, 12); table.setFont(font); table.setRowHeight(30); JFrame frame = new JFrame(); frame.setSize(600, 400); frame.add(new JScrollPane(table)); frame.setVisible(true); } }
This will produce the following output −
- Related Articles
- Java Program to create DefaultTableModel from two dimensional array
- How to create a two dimensional array in JavaScript?
- How to create a two-dimensional array in TypeScript?
- Create new instance of a Two-Dimensional array with Java Reflection Method
- How to declare a two-dimensional array in C#
- How do we access elements from the two-dimensional array in C#?
- How to set multidimensional array into JTable with Java?
- Split one-dimensional array into two-dimensional array JavaScript
- How to create and populate two-dimension Java array?
- Single dimensional array in Java
- How to create LabelValue Tuple from an array in Java
- How to create an ArrayList from an Array in Java?
- How to create a subarray from another array in Java
- How to access elements from multi-dimensional array in C#?
- How to perform arithmetic operations on two-dimensional array in C?

Advertisements