Java JTable For Listing

Java JTable~Here we’ll create a table or especially a JTable and set a list of people and their Hobby’s. This JTable is really good to use when you have a lot of peoples data to list for. You’ll create it into a form of columns and rows.

Here is the code :

import javax.swing.*;    
public class TableExample {
JFrame f;
TableExample(){
f=new JFrame();
String data[][]={ {"101","Anny","Swimming"},
{"102","Bob","Cycling"},
{"103","James","Football"},
{"104","Zoey", "Singing"}
}; //here you can add data to the table
String column[]={"List","Name","Hobby"}; /*here you can set the column data name inside the table*/
JTable jt=new JTable(data,column);
jt.setBounds(30,40,200,300);
JScrollPane sp=new JScrollPane(jt);
f.add(sp);
f.setSize(300,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TableExample();
}
}

and here’s the output :

java JTable

Whats great is that, when you inputs a lot of data at once.. the JTable will automatically insert a scroll, so you won’t have to code it yourself. Like this :

Java JTable

Be careful, that when you don’t input a data and leave a column empty in Java JTable like this :

 String data[][]={   {"101","Anny","Swimming"},    
{"102","Bob","Cycling"},
{"103","James","Football"},
{"104","Zoey"}
};

You’ll program will not work properly and it will show you errors. So make sure that if you have 3 columns to fill, you filled all of that 3 columns.

Published by

Rafif Santika

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.