MySQL Database and Java Desktop GUI Part 3
MySQL Database and Java Desktop GUI Part 3
Contents:
1. Machine specifications used in this task
2. Pre requirement
3. Adding a Print Table Feature
4. Adding the Print Table Code
5. Customizing the Menu Items
6. Solving the Date Of Birth Issue: Editing the Table
7. Adding Static Text and Image
8. Adding Panel, Labels, Static Text And Image
9. Adding radio button
10. Running and Testing
11. Customizing the Frame and the About Page.
12. Final Note
In this tutorial we continue playing with NetBeans and Java desktop GUI project,
continue to customize our GUI by setting up the table printing, adding menu items,
image, static text, button, panel, codes for event handling and so on. On the way we
will try to solve the DOB issue which we will change the data type
from DATE to VARCHAR. Appreciate the steps on how to edit the original table
properties while designing the GUI interface. Our main purpose actually to be familiar
with the NetBeans IDE features.
Machine specifications used in this task
Intel Pentium Core 2 Duo, 2.2 GHz.
Windows XP Pro SP2 + periodical patches + periodical updates.
2 GB DDR2 RAM
160 GB SATA HDD
17 SyncMaster 713N monitor.
Pre requirement: NetBeans 6.x.x, MySQL 5.x.x and Already completed Tutorial 2
Adding a Print Table Feature
1. Let do it. Firstly we would like to add a Print button to print a table. Drag and drop
the jButton on the design canvas on the right most, above the table.
mode.
5. Next we re-position all the buttons at the top, inline with the Print button
just above the table and re-arranged other components, leaving the top
area an empty space for other components that will be added later. This is
a drag and drop task.
6. Next we would like to add the event handler to the Print button, which
means action to done when the button clicked event happened. Select the
Print button > right click mouse > select Event > select Action > select
actionPerformed.
2. Add the following import directives for the classes (methods) that we need
//table.print(JTable.PrintMode.FIT_WIDTH,
header, null);
masterTable.print(JTable.PrintMode.FIT_WIDTH,
header, footer);
} catch (java.awt.print.PrinterException e) {
System.err.format("Cannot print %s%n",
e.getMessage());
}
}
4. Actually, you can try completing the code first instead of adding the import
directives and then use the Fix Imports context menu as shown in the
following Figure. We will use this feature frequently in the Java web project.
6. Drag and drop the Menu Item component between the Refresh and Exit
menu items.
2. Repeat the same steps for other menu items. The following is for the
5. Next we add a separator between the Refresh and the Print menu items.
6. Drag and drop a separator between Refresh and Print men items.
7. Assign an event handler when user clicks the Print menu by invoking the
handler skeleton provided for us that ready for our customized codes.
//table.print(JTable.PrintMode.FIT_WIDTH,
header, null);
masterTable.print(JTable.PrintMode.FIT_WIDTH,
header, footer);
} catch (java.awt.print.PrinterException e) {
System.err.format("Cannot print %s%n",
e.getMessage());
}
10. Re-build and re-run the project. Select the Print sub menu under the File
11. The following message box displayed when there is no printer driver
12. Dont forget also to test the Print button. The following Figure shows the
print dialog box. In this case we would like to print the table to PDF file. All
printers that available in the machine where this program run will be
displayed in the Name: drop down list. Click the Print button.
14. The following is a sample of the table printed to the PDF file.
First of all we need to edit the DOB data type using MySQL Query Browser. Launch
MySQL Query Browser.
1. Select the database in the Schemata windows > right click mouse > select
3. Next, select the table > right click mouse > select Edit Table context menu.
5. Click the Apply Changes button. Click Execute for the Table Edit
6. Next, re-include the DOB field in the Java GUI. Add Label and text field to
the GUI and put them between the Name and Address fields.
7. Next, bind to the Master Table data. Select the DOB text field > right click
OK.
9. Next, add the DOB field/column into the table. Select table > right click
11. Click the Insert button and select the newly added row.
12. Change the Title: and Expression: as shown in the following Figure.
13. Use the Move Up button to move the DOB row between Name and
14. The Date Of Birth was added to the table as shown in the following Figure.
the JMenuBar. In the panel we drag and drop three labels, one for logo
image (2) and another two for the static texts (3 and 4).
2. To load the image, use the icon property of the Jlabel (2) in Properties
page. Any image that saved under the resources folder will be visible in
the drop down list.
5. Our final appearance will look something like the following. You can
6. We continue customizing the look and feel such as colour, border, text etc.
for the existing components. This steps just to practise what we have done
previously.
Adding Radio Button
1. Drag and drop two radio buttons on the right side of the Marital Statuss
text field so that when user select a radio button, the value of 0 or 1 will be
displayed in the Marital Statuss text field.
3. We need to group these radio buttons so that at any time only one radio
button will be selected. Drag and drop the Button Group component on the
panel. You cant see it in the panel but available in the Inspector window.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4. Multiple select the radio buttons (press down Ctrl button and multiple
6. Bind the radio buttons to the Marital Status master table column. Select
the first radio button and right-click mouse. Select Bind > select selected.
2. To run this application from the command line without Ant, try:
java -jar
"C:\myjavaproject\uumstudentrecordUI\dist\uumstudentrecordUI
.jar"
3. Invoking the printing feature. Click the Print button or Print sub menu.
2. In our case we edit the keys to the following values (leave the
3. Run your project and invoke the About sub menu. Notice that the frames
4. Next, we are going to customize the image in the About page. In this case
we use Adobe Photoshop CS3. You can use other image editors as well.
5. Open the original image, about.png under the resources folder in the
Photoshop (or any image editor). You can also create your own image from
scratch. The splash.png image will be used for installation/deployment.
6. We edit the original image to the following colors and save to its original
7. Run our project and invoke the About sub menu. The new image was
Final Note
Well, that all folks! A very long journey and fun, playing with Java desktop GUI that
connected to MySQL database using NetBeans. In the next episodes we will explore
Java web applications and we found that web applications are more appropriate to be
developed because web applications can serve both enterprise and web users.