- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 add some rows to a table through DefaultTableModel in Java
Let us first create a demo table −
JTable table = new JTable(tableModel);
Now, add a column to it −
tableModel.addColumn("Languages");
Add some rows to the table using insertRow() method −
tableModel.insertRow(0, new Object[] { "CSS" }); tableModel.insertRow(0, new Object[] { "HTML5" }); tableModel.insertRow(0, new Object[] { "JavaScript" }); tableModel.insertRow(0, new Object[] { "jQuery" }); tableModel.insertRow(0, new Object[] { "AngularJS" });
The following is an example to add a row to a table with DefaultTableModel −
Example
package my; 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(); JTable table = new JTable(tableModel); tableModel.addColumn("Languages"); tableModel.insertRow(0, new Object[] { "CSS" }); tableModel.insertRow(0, new Object[] { "HTML5" }); tableModel.insertRow(0, new Object[] { "JavaScript" }); tableModel.insertRow(0, new Object[] { "jQuery" }); tableModel.insertRow(0, new Object[] { "AngularJS" }); tableModel.insertRow(tableModel.getRowCount(), new Object[] { "ExpressJS" }); JFrame f = new JFrame(); f.setSize(550, 350); f.add(new JScrollPane(table)); f.setVisible(true); } }
This will produce the following output −
- Related Articles
- How to add rows to a table using JavaScript DOM?
- How to optimize a MySQL table after deleting some rows?
- Java Program to remove the first row from a table with DefaultTableModel
- Java Program to remove the last row from a table with DefaultTableModel
- How to add table rows Dynamically in Android Layout?
- How to deselect a range of rows from a table in Java?
- How add unique key to existing table (with non-unique rows)?
- Inserting rows to an internal table through debug in SAP ABAP
- How can I loop through all rows of a table in MySQL?
- How to highlight multiple rows in a sequence in a table with Java Swing?
- How to sort rows in a table using JavaScript?
- Java Program to create DefaultTableModel from two dimensional array
- How to delete all rows except some in MySQL?
- Selecting and displaying only some rows from a column in a MySQL table
- Delete only some rows from a table based on a condition in MySQL

Advertisements