- 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 add JTable to Panel in Java Swing?
To add JTabel to Panel, let us first crerate a panel −
JPanel panel = new JPanel();
Now, create JTable and add rows and columns with the records −
String[][] rec = { { "1", "Steve", "AUS" }, { "2", "Virat", "IND" }, { "3", "Kane", "NZ" }, { "4", "David", "AUS" }, { "5", "Ben", "ENG" }, { "6", "Eion", "ENG" }, }; String[] header = { "Rank", "Player", "Country" }; JTable table = new JTable(rec, header);
Add the above created table to panel −
panel.add(new JScrollPane(table));
The following is an example to add JTabel to Panel in Java Swing −
Example
package my; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.TitledBorder; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "ODI Rankings", TitledBorder.CENTER, TitledBorder.TOP)); String[][] rec = { { "1", "Steve", "AUS" }, { "2", "Virat", "IND" }, { "3", "Kane", "NZ" }, { "4", "David", "AUS" }, { "5", "Ben", "ENG" }, { "6", "Eion", "ENG" }, }; String[] header = { "Rank", "Player", "Country" }; JTable table = new JTable(rec, header); panel.add(new JScrollPane(table)); frame.add(panel); frame.setSize(550, 400); frame.setVisible(true); } }
This will produce the following output −
- Related Articles
- How to add a title to JTable in Java Swing?
- Java Program to add Titled Border to Panel in Swing
- How to add a new row to JTable with insertRow() in Java Swing
- Java Program to append a row to a JTable in Java Swing
- How to get the number of rows and columns of a JTable in Java Swing
- Move the first row to the end of the JTable in Java Swing
- Java Program to add Combo Box to JTable
- How can we add/insert a JButton to JTable cell in Java?
- How can we add multiple sub-panels to the main panel in Java?
- How can we add/insert a JRadioButton to a JTable cell in Java?\n
- How to maximize JFrame in Java Swing
- How to apply adjustments to the last column of a JTable only, when the width of any column is changed in Java Swing?
- How to apply adjustments to the next column of a JTable only, when the width of a column is changed in Java Swing?
- How to change Button Border in Java Swing
- How to make a canvas in Java Swing?

Advertisements