
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Set Multidimensional Array into JTable with Java
To set multidimensional array into a table, we need the values for rows and columns. Therefore, create a multidimensional array for rows −
Integer[][] marks = { { 70, 66, 76, 89, 67, 98 }, { 67, 89, 64, 78, 59, 78 }, { 68, 87, 71, 65, 87, 86 }, { 80, 56, 89, 98, 59, 56 }, { 75, 95, 90, 73, 57, 79 }, { 69, 49, 56, 78, 76, 77 } };
Now, columns −
String students[] = { "S1", "S2", "S3", "S4", "S5", "S6"};
Add the rows and columns set above to the table −
JTable table = new JTable(marks, students);
The following is an example to set multidimensional array into JTable −
Example
package my; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; public class SwingDemo { public static void main(String[] argv) throws Exception { Integer[][] marks = { { 70, 66, 76, 89, 67, 98 }, { 67, 89, 64, 78, 59, 78 }, { 68, 87, 71, 65, 87, 86 }, { 80, 56, 89, 98, 59, 56 }, { 75, 95, 90, 73, 57, 79 }, { 69, 49, 56, 78, 76, 77 } }; String students[] = { "S1", "S2", "S3", "S4", "S5", "S6"}; JTable table = new JTable(marks, students); 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); } }
The output is as follows displaying student marks in a JTable with multidimensional array −
Output
Advertisements