This tutorial will teach you how to load your SQLite table values to Java Swing JTable using NetBeans IDE. Before following the tutorial, you must have an SQLite table with at least 1 row.
First, we need to create a new project. Click “File” from the menu bar on top. Choose “New Project” or alternatively you can open New Project by pressing and holding the keys Ctrl+Shift+N. This will open the New Project dialogue box.
Pay no attention to the theme because I’m using a different theme. Once you’re in the “New Project” dialogue box, choose “Java” from the Categories on the left side and under “Projects” choose “Java Application” and click “Next >”.
This will take you to the “New Java Application” dialogue box. Fill the fields like so:
This dialogue box is where we can set the name of our project, its location, and Main Class creation. For the meantime, leave everything as it is except for the project name and the “Create Main Class” checkbox.
Let’s name our project “JTable using rs2xml” and un-tick the “Create Main Class” checkbox then click “Finish”.
The dialogue box will close and on the left side of NetBeans, under “Projects”, you’ll see that our project has been created.
It is not recommended to start your project with a default package so right click on the “Sources Package”, hover to New and click “Java Package…” and name it “main”.
Now we’ll add a new JFrame form. JFrame form is what will contain all of our controls. To add one, right click under Projects, hover to New then click “New JFrame Form”. The dialogue box below will show. Let’s rename the frame with “JTableTutu” by typing it into the Class Name field then click “Finish” below.
On the right-hand side of your NetBeans Window, you’ll see the “Palette”.
Let’s first add the JTable. We can add Jtable to our JFrame by clicking “Table” from the Palette under “Swing Controls”. Hover back to the JFrame form, drop the JTable inside it. You can resize it to any size you want even the JFrame form itself.
In the same manner, we’ll add a JButton. Click Button from the Palette and add it on top of your JTable. You should have a similar setup like below by now. The image below shows the JButton has a text JButton2, yours may be JButton1.
Now we will add the codes. Currently, we are on the “Design” Page. To add the codes, we’ll have to navigate to “Source” page. There is more than 1 way to navigate to it but for now, we’ll stick with the conventional way. Click “Source” just beside “Design” located just above your JFrame form.
Just under the package declaration, paste the necessary files to be imported just like image below.
The light bulbs below it say all the imports are unused. This is normal because we haven’t added the codes yet that will use them. To start using them, paste the codes below under the class declaration.
Next is load the database values to the JTable. Navigate back to “Design” and double click on the JButton and this will load actionPerformed event on your JButton. Paste the codes below.
pstmt = conn.prepareStatement(“Select * from test”);
rs = pstmt.executeQuery();
Note that this particular line of codes: (“Select * from test”); is getting the “test” table from my SQLite table. You may have a different table name. To call your table value, just replace the “test” with the name of your table.
Now run your project by pressing F6 or by clicking the “Run” button.
If it asked you to choose a Main Class, just choose the name of your class.
When your project loads, click the JButton and your database values should now be displayed on your JTable.