

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 append a row to a JTable in Java Swing
To append a row, you can use the addRow() method. Ley us first create a table wherein we need to append a row −
DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel);
Add some columns to the table −
tableModel.addColumn("Language/ Technology"); tableModel.addColumn("Difficulty Level");
Add some rows −
tableModel.insertRow(0, new Object[] { "CSS", "Easy" }); tableModel.insertRow(0, new Object[] { "HTML5", "Easy"}); tableModel.insertRow(0, new Object[] { "JavaScript", "Intermediate" }); tableModel.insertRow(0, new Object[] { "jQuery", "Intermediate" }); tableModel.insertRow(0, new Object[] { "AngularJS", "Difficult"});
Now, if you need to append a row to the table we created above, use the addrow() method −
tableModel.addRow(new Object[] { "WordPress", "Easy" });
The following is an example to append a row to a JTable −
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("Language/ Technology"); tableModel.addColumn("Difficulty Level"); tableModel.insertRow(0, new Object[] { "CSS", "Easy" }); tableModel.insertRow(0, new Object[] { "HTML5", "Easy"}); tableModel.insertRow(0, new Object[] { "JavaScript", "Intermediate" }); tableModel.insertRow(0, new Object[] { "jQuery", "Intermediate" }); tableModel.insertRow(0, new Object[] { "AngularJS", "Difficult"}); // adding a new row tableModel.insertRow(tableModel.getRowCount(), new Object[] { "ExpressJS", "Intermediate" }); // appending a new row tableModel.addRow(new Object[] { "WordPress", "Easy" }); JFrame f = new JFrame(); f.setSize(550, 350); f.add(new JScrollPane(table)); f.setVisible(true); } }
Output
- Related Questions & Answers
- How to add a new row to JTable with insertRow() in Java Swing
- How to add a title to JTable in Java Swing?
- Java Program to increase the row height in a JTable
- Move the first row to the end of the JTable in Java Swing
- How to add JTable to Panel in Java Swing?
- How to highlight a row in a table with Java Swing?
- How to enable row selection in a JTable with Java
- Java Program to select a column in JTable?
- How to display row count in Java Swing JList
- Java Program to deselect all cells in a JTable
- Java Program to enable cell selection in a JTable
- Java Program to enable column selection in a JTable
- How to get the number of rows and columns of a JTable in Java Swing
- Java Program to set the height of only a single row in a JTable with multiple rows
- Java Program to deselect a range of columns in a JTable
Advertisements