Lab6 Netbeans
Lab6 Netbeans
- Click on New Project, input the project’s Name, Location, Language, and JDK, and
click Create. You may need JDK installed first or let Netbeans IDEA install for you.
- Familiarize yourself with the workspace of NetBeans IDEA. The left sidebar is the
project's directory; the central part is where you code. Also note the Run button, which
will compile your code and Terminal window for debugging purposes.
- Now delete the Main.java class by right-clicking on the file name, then click on Delete
and OK to the pop-up window. We will construct these classes later.
- Lastly, we must import the driver and libraries into the project scope. Right click on
libraries -> Add Jar/Folder to begin the importing process.
- Then click the Libraries tab on the left sidebar and New Project Library -> Java to add
the libraries.
- Point to the JAR libraries inside the Lab 5 folder. Then click OK to add them to the
project. The result should look like this. Then click Apply, OK to finish this step.
2. Set up the Swing UI Form.
- Before we can get to the coding, we need to design the GUI (Graphical User Interface)
Frame to provide a frontend for users to interact with. Right-click the package (pdm06)
folder, then select New -> JFrame Form
- Give the form name and click OK to complete the frame creation.
- Now click the minus button on the sidebar to minimize it and get familiar with the GUI
Design form. The left side has a Component Tree to help you keep track of the
components in the Form and has a default JPanel object as a default. Below is the
property and value key-value pair to modify attributes like name, fonts, size, etc. In the
middle, we have the current frame design preview, and in the rightmost sidebar, we have
a Palette with all the components we need to construct the GUI.
- Here is the list of some standard Java Swing components:
o HSpacer, VSpacer: to add spacing between the elements.
o JPanel: to add a frame for other elements, it must be extended from our frame
classes.
o JButton: add a button with ActionListeners to perform logical code when
clicking (discuss more in the later Lab).
o JScrollPane: add a frame that can be scrollable, especially for extended content.
o JLabel: to add title and instruction on the interface.
o JTextField, JPasswordField: to receive user input on the interface; the latter is
used for the password field to protect privacy.
o JTextArea: to display the message after the query has been done.
o JComboBox: to make a drop-down list for the user to choose from.
o JTable: to display the query result in tabular form.
o JOptionPane: to display the dialog for the user to interact with.
- Drag the corresponding element from the Palette to the main preview frame in the middle
to add a component. Here is an example of adding a JLabel element.
- From then, you must modify its value by double-clicking the Label text in the main
frame, typing new text, and pressing Enter/Return to complete.
- You must also rename the Variable Name component in the code for easy tracking.
Modify it using the property field name and type it in the value box as you want.
- Then, do the same for other components until you get a proper GUI to display to the user.
Design a GUI form as in the Figure below.
- Examine and copy the code to the file you created, then let IntelliJ IDEA auto-import the
libraries in the code for you.
import net.proteanit.sql.DbUtils;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.sql.*;
import java.util.Vector;
- Copy the following code after the variable initialization lines, then examine and complete
the code by replacing <?> with your answer.
private frmExplorer() {
setContentPane(panel);
setTitle(<?>);
setSize(<?>,<?>);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.<?>);
}
- Use these questions to guide you:
1. What should be named as the title for this frame?
2. What is the size of this frame window?
3. On close operation, what action should be taken from the WindowConstants class?
- Now it’s time to add an event code for the button. Normally we would right-click the
JButton element, choose Create Listener -> Action Listener, and click OK to finish
the step.
- However, to make the code more robust, we will implement a Design Pattern –
SwingWorker here. Inside the private function frmExplorer(), copy the code below and
complete it by replacing <?> with your answer.
<?>.addActionListener(
e -> {
<?>.setEnabled(false);
if (<?>.getText().<?>) {
JOptionPane.showMessageDialog(
null, <?>, <?>, JOptionPane.WARNING_MESSAGE);
<?>.setEnabled(true);
return;
}
SwingWorker<Void, Void> worker =
new SwingWorker<>() {
@Override
protected Void doInBackground() {
<?>.showQuery(<?>.getText(), <?>);
return null;
}
@Override
protected void done() {
<?>.setEnabled(true);
}
};
worker.execute();
});