Display Results In a JTable

What is a JTable?

JTable is a swing component that already placed in the java libraries.In the database projects this component takes a viral role. But lots of students have no idea how this works. First of all you have to drag and drop JTable from your swing palette.

And also there is a class called Vector. This Vector class behaves like an array. It can hold huge amount of data rather than array and it is very easy to get those data from the vector class. You can see in the example how that vector class took placed in the project. But I think it is somewhat hard to understand that classes.But search something on the internet and find some factors about vector class.

I have added a new JFrame called table and I added JTable component into my frame. You can see in the picture where the JTable placed.

I have renamed my JTable as tableResult and I am going to fill that table using Vector class from my MySQL database. Let’s we see how it happen.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 

connection con=new connection();
Connection getcon=null;
Vector col = new Vector();
Vector dat= new Vector();
ResultSet rs = null;
try {
getcon = con.creatConnection();
col.add("Id");
col.add("Title");
col.add("First Name");
col.add("Last Name");// create contact default colum names and sore it
col.add("Nick Name");//This texts will display in the header of the table
col.add("E-Mail");
rs=getcon.createStatement().executeQuery("select*from contactinformation"); //getting all                                the information from the table

dat.clear(); //clear the object before use it

while(rs.next())   {// if record source avilable

Vector v =new Vector();
v.add(rs.getString("ContactId").trim());
v.add(rs.getString("title").trim());
v.add(rs.getString("fName").trim());
v.add(rs.getString("lName").trim());// getting contacts from database and store in object
v.add(rs.getString("nName").trim());
v.add(rs.getString("email").trim());

dat.add(v);
}
tableResult.setModel(new DefaultTableModel(dat, col)); //finnaly set all the values to the database
}
catch(Exception ex)     {

JOptionPane.showMessageDialog(null, ex.getMessage());

}

// TODO add your handling code here:
}

You must have a result something like this

If you have a problem regurding the example let me know it.OK enjoy the example

Download Complete Example

6 thoughts on “Display Results In a JTable

  1. this was very helpfull i spent 2 days searching for this kind of answer i am amazed it is still relevant even afte all these years! thank you very much

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s