How to show database data in JScrollPane and JTable in Java Swing/AWT/SWT/Windows Builder.

Last modified date

Design your frame with JScrollPane and Merge you JTable within the scrollpane. You must remember that never Merge 2 tables with single JScrollPane it will not work, because one table can only merge scrollpane.

Here I’ll show you an example how to merge you JScrollPane with JTable and later I show how to show Database data in JTable using a query writing in a method.

Here is an example code how to merge scrollpane with JTable

    JTable table=new JTable();
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(97, 72, 218, 136);

Now I will write a method to show Database data in table.

Connection connection=null;
public void showDataInTable()
  try {
    String query="select * from emp";
    Statement st=connection.createStatement();
    ResultSet set=st.executeQuery(query);
    // Dont call While loop or if condition here
  } catch (Exception e) {
    // TODO: handle exception

It depends how and on what action performed we would like to show the data, if we want to show the JTable data at the time of application startup. Just call it in your JFrame or JDialog constructor at end or if you want to call it on any action performed on JButton Click just go with the below code.

JButton btnShowData = new JButton("show Data");
btnShowData.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
btnShowData.setBounds(226, 38, 89, 23);

If you clearly observe the above method I did not called the While loop or if condition to show at the point after ResultSet. Even if you call the ‘While’ Loop and ‘if’ condition the data will be displayed in the JTable but the first row of your database will not be shown.

Now we will discuss what DbUtils is, it is present is in rs2xml.jar library which helps us to show the Database data into your JTable. So download it from internet and add it to you user library to run this.

I use the same process to display the Database table data in JTable.

Good Luck!