(Solved) : Intellj Java Project Result Look Like Create Swing Gui Displaying Selected Customer Inform Q35631460 . . .

IntellJ Java Project  

Result should look like: FiredUp Customers View All Customers View Customers from State: Name City State

create a Swing GUI for displaying selected customer informationfrom the FiredUp database. You do not need to connect your GUI tothe database , just design the user interface.

The GUI should support 2 functions: (1) displaying data from allcustomers, and (2) displaying data from customers in a given state.The customer attributes to be displayed should include Name, City,and State (don’t worry about any other attributes). For this lab,when the user engages function (1) or (2), pop up a message box(use JOptionPane) stating that the given function is notimplemented yet.

A few tips:

  • There should be a way for the user to select whether they wantto view data on all customers or only those from a given state.There will need to be a way for the user to enter the state theyare interested in.
  • Use a JTable to display the customer data, and put the tableinside a scroll pane: In GUI Designer, drag a JScrollPane onto theform, and then drag a JTable onto the scroll pane.
  • The customer table should have columns for Name, City, andState. Since you aren’t connecting to the database yet, you don’tneed to show any data in the table, but you can put in fake,hard-coded data if you want to. The column headers should show upwhether there is data or not.
    • JTable is not covered in the textbook. To learn how to workwith tables, see this Simple JTable Example or check out the fullJava Tutorial on How to Use Tables. You should also review the APIdocumentation for javax.swing.JTable. It is common for a programmerto need to investigate a new component in order to learn how to useit. This is a good exercise in that skill. Feel free to post in thediscussion or email the instructor with questions if you getstuck.
    • You may use the given CustomersTableModel as the model for yourtable. Note that you will need to add the Customer class from theLesson 4 JDBC tutorial to your project.
    • ————————

public class Main {
public static voidmain(String[] args) {
System.out.println(“FiredUp Customers:”);
FiredUpDB firedUp =new FiredUpDB();
List<Customer>customers = firedUp.readCustomers();
for(Customer cust : customers) {
System.out.println(“ID: “ + cust.getId() +
“, Name: “ + cust.getName() +
“, City: “ + cust.getCity() +
“, State: “ + cust.getState() +
“,Email Address(es): “ + cust.getEmailAddresses()); }}}
——————————————————————–

import java.util.ArrayList; public class Customer { private int id; private String name; private String city; private String state; private ArrayList<String> emailAddresses; public Customer(int id, String name, String city, String state) { this.id = id; this.name = name; this.city = city; this.state = state; emailAddresses = new ArrayList<>(); } public int getId() { return id; } public String getName() { return name; } public String getCity() { return city; } public String getState() { return state; } /** * Add an email address to this customer’s email addresses * @param email an email address belonging to this customer */ public void addEmailAddress(String email) { emailAddresses.add(email); } /** * @return the list of email addresses belonging to this customer */ public ArrayList<String> getEmailAddresses() { return emailAddresses; }}

—————————————————————-

import java.sql.*;import java.util.ArrayList;import java.util.List; public class FiredUpDB { private static final String FIREDUP_URL = “jdbc:jtds:sqlserver://cisdbss.pcc.edu/FiredUp”; private static final String USERNAME = “275student”; private static final String PASSWORD = “275student”; private static final String CUSTOMER_SQL = “SELECT CustomerID, Name, City, StateProvince FROM CUSTOMER”; private static final String EMAIL_SQL = “SELECT EmailAddress FROM EMAIL WHERE FK_CustomerID = ?”; /** * Read all customers from the FiredUp database and return them as a list of Customer objects * @return a list of customers from the FiredUp database */ public List<Customer> readCustomers() { ArrayList<Customer> customers = readCustomerBasics(); readEmailAddresses(customers); return customers; } /** * Read from the FiredUp database customers from the given state * @param state the state of interest (US state or Canadian province) * @return a list of customers from the given state */ // Create method readCustomersFromState /** * @return a connection to the FiredUp database * @throws SQLException if unable to connect */ private Connection getConnection() throws SQLException { return DriverManager.getConnection(FIREDUP_URL, USERNAME, PASSWORD);} /** * Read customers from the database, including their basic properties from the CUSTOMER table, * but not including customer data from related tables such as email addresses or phone numbers * @return a list of Customer objects */ private ArrayList<Customer> readCustomerBasics() { ArrayList<Customer> customers = new ArrayList<>(); try ( Connection conn = getConnection(); PreparedStatement stmt = conn.prepareStatement(CUSTOMER_SQL); ResultSet rs = stmt.executeQuery() ) { while (rs.next()) { customers.add(new Customer(rs.getInt(“CustomerID”), rs.getString(“Name”), rs.getString(“City”), rs.getString(“StateProvince”))); } } catch (SQLException e) { e.printStackTrace(); } return customers; } /** * Read email addresses from the database for each customer in the given list, * adding the email addresses found to the corresponding Customer object * @param customers list of customers whose email addresses should be read */ private void readEmailAddresses(ArrayList<Customer> customers) { try ( Connection conn = getConnection(); PreparedStatement stmt = conn.prepareStatement(EMAIL_SQL) ) { for (Customer cust : customers) { stmt.setInt(1, cust.getId()); ResultSet rs = stmt.executeQuery(); while (rs.next()) { cust.addEmailAddress(rs.getString(“EmailAddress”)); } } } catch (SQLException e) { e.printStackTrace(); } }}

—————————————————————————————–

CustomersTableModel source code:

(Note you need to import java.util.List. There is also ajava.awt.List, but that is not the one used below.)

private class CustomersTableModel extends DefaultTableModel{

private final String[] COL_NAMES = {“Name”,”City”, “State”};

private List<Customer> customers;

publicCustomersTableModel(List<Customer> customers) {

super();

this.customers =customers;

setColumnIdentifiers(COL_NAMES);

setRowCount(customers.size());

}

public void setCustomers(List<Customer>customers) {

this.customers =customers;

setRowCount(customers.size()); }

@Override

public Object getValueAt(int row, int col){

switch(col) {

case 0: return customers.get(row).getName();

case 1: return customers.get(row).getCity();

case 2: return customers.get(row).getState();

default: return null; } }

@Override

public boolean isCellEditable(int row, intcol) {

returnfalse;    } }

FiredUp Customers View All Customers View Customers from State: Name City State Show transcribed image text FiredUp Customers View All Customers View Customers from State: Name City State

Expert Answer


import java.awt.EventQueue;

import javax.swing.JFrame; . . .