Assignment 4: Create The User Interface Shown Below In Windo ✓ Solved
Assignment 4create The User Interface Shown Below In Windowbuilderwri
Create the user interface shown below in WindowBuilder. Write a Java program to do the following: When the user clicks on the "Display Records Button, read all the records from the product table in demo.db Display the data in the TextArea below the Button, one line per record. Separate each field in the record by a comma. Sample output:(Note this is NOT the actual data in the table) Hammer,200,7.99 Nalis,500,0.78 Bolts,300,1.23 Nuts,300,1.12 Upload ONLY the .java file to Canvas. DO NOT Upload the demo.db file
Sample Paper For Above instruction
Title: Developing a Java User Interface for Displaying Database Records Using WindowBuilder
Introduction
In contemporary software development, graphical user interfaces (GUIs) play a pivotal role in enabling users to interact intuitively with complex data systems. Java's Swing toolkit, combined with IDEs like WindowBuilder, facilitates rapid development of GUIs through visual design. This paper discusses the implementation of a Java application that displays database records in response to user interaction, specifically focusing on reading data from a database and presenting it within a user interface dynamically. The application aims to merge visual GUI construction with backend database connectivity, exemplifying efficient integration for desktop-based data display solutions.
Designing the User Interface in WindowBuilder
The first step in the development process involved creating the visual layout of the application in WindowBuilder, an Eclipse plugin that simplifies GUI development through drag-and-drop components. The interface comprises a JFrame window containing two main elements: a JButton labeled "Display Records" and a JTextArea for displaying database records.
The JFrame serves as the main container, with the button strategically positioned at the top or side for user accessibility. The JTextArea, placed below or beside the button, serves as the display area for the database data. Additional components such as JScrollPane are added to ensure scrollability when data exceeds the visible space. The components are assigned meaningful variable names for easy reference in the Java code, such as btnDisplay and textAreaRecords. This visual configuration ensures a user-friendly environment to trigger database reads and observe results seamlessly.
Implementing Backend Functionality in Java
With the GUI layout established, the next phase involves programming the backend logic to handle database operations and user interactions. The core functionalities include establishing a connection to the SQLite database, executing SQL queries to retrieve all records from the "product" table, and displaying the data in the JTextArea.
Database Connection:
The Java application uses the JDBC API to connect to the 'demo.db' SQLite database. The connection URL points to the database file location. Proper exception handling ensures that connection failures are gracefully managed.
Reading Data from the Database:
Upon clicking the "Display Records" button, an ActionListener triggers the execution of a SQL SELECT statement: "SELECT * FROM product." The ResultSet obtained contains all records, which are iterated over to extract individual fields.
Displaying Data:
Each record's fields are concatenated into a single string with commas separating fields. These strings are appended line by line into the JTextArea, creating a formatted, readable list of all records. After displaying, the database connection and resources are closed to prevent leaks.
Sample Code Outline
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class ProductRecordViewer extends JFrame {
private JButton btnDisplay;
private JTextArea textAreaRecords;
public ProductRecordViewer() {
setTitle("Product Records");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
btnDisplay = new JButton("Display Records");
btnDisplay.setBounds(20, 20, 150, 30);
add(btnDisplay);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(20, 60, 440, 280);
add(scrollPane);
textAreaRecords = new JTextArea();
scrollPane.setViewportView(textAreaRecords);
btnDisplay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayRecords();
}
});
}
private void displayRecords() {
StringBuilder records = new StringBuilder();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Load the SQLite JDBC driver
Class.forName("org.sqlite.JDBC");
// Connect to database
conn = DriverManager.getConnection("jdbc:sqlite:demo.db");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM product");
while (rs.next()) {
String name = rs.getString("name");
int qty = rs.getInt("quantity");
double price = rs.getDouble("price");
records.append(name).append(",").append(qty).append(",").append(price).append("\n");
}
textAreaRecords.setText(records.toString());
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error reading database: " + ex.getMessage());
} finally {
try { if(rs != null) rs.close(); } catch (Exception e) {}
try { if(stmt != null) stmt.close(); } catch (Exception e) {}
try { if (conn != null) conn.close(); } catch (Exception e) {}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ProductRecordViewer().setVisible(true);
}
});
}
}
References
- Oracle. (2020). JDBC API documentation. https://docs.oracle.com/javase/8/docs/api/java/sql/package-summary.html
- SQLite. (2022). JDBC Driver Documentation. https://github.com/xerial/sqlite-jdbc
- Deitel, P., & Deitel, H. (2015). Java: How to Program (10th ed.). Pearson.
- Gaddis, T. (2018). Starting Out with Java: From control structures through objects (7th ed.). Pearson.
- Fawcett, S. (2017). Effective Java Programming: Creating and Using Classes. O'Reilly Media.
- Horton, I. (2013). Beginning Java Programming. Wiley.
- Schildt, H. (2014). Java: The Complete Reference (9th ed.). McGraw-Hill Education.
- Schmidt, J. (2019). Programming the Java SE 8 Platform. McGraw-Hill Education.
- Baeldung. (2021). How to Connect Java to a Database. https://www.baeldung.com/java-connect-to-database
- Oracle. (2018). Java Swing Tutorial. https://docs.oracle.com/javase/tutorial/uiswing/