- 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
Java Program to create DefaultTableModel from two dimensional array
The DefaultTableModel is an implementation of TableModel that uses a Vector of Vectors to store the cell value objects. At first create a two dimensional array for rows and columns −
DefaultTableModel tableModel = new DefaultTableModel(new Object[][] { { "India", "Asia" }, { "Canada", "North America" }, { "Singapore", "Asia" }, { "Malaysia", "Asia" }, { "Philippins", "Asia" }, { "Oman", "Asia" }, { "Germany", "Europe" }, { "France", "Europe" } }, new Object[] { "Country", "Continent" });
Above, “Country” and “Continent” are the columns. Now, set the above set of rows and columns to JTable −
JTable table = new JTable(tableModel);
The following is an example to create DefaultTableModel 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[][] { { "India", "Asia" }, { "Canada", "North America" }, { "Singapore", "Asia" }, { "Malaysia", "Asia" }, { "Philippins", "Asia" }, { "Oman", "Asia" }, { "Germany", "Europe" }, { "France", "Europe" } }, new Object[] { "Country", "Continent" }); 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); } }
Output
- Related Articles
- How to create JTable from two dimensional array in Java?
- 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
- Java Program to remove the first row from a table with DefaultTableModel
- Java Program to remove the last row from a table with DefaultTableModel
- C# program to Loop over a two dimensional array
- Golang Program to read and print two-dimensional array
- Java Program to create Character Array from String Objects
- Java Program to convert array to String for one dimensional and multi-dimensional arrays
- Split one-dimensional array into two-dimensional array JavaScript
- Java Program to create Stream from a String/Byte Array
- Java Program to Add Two Matrix Using Multi-Dimensional Arrays
- Java program to create a sorted merged array of two unsorted arrays
- C Program on two-dimensional array initialized at run time

Advertisements