{ Dating Service ActionHandler Listener class details

Preview:

Citation preview

{{Dating ServiceDating Service

ActionHandler Listener class ActionHandler Listener class detailsdetails

class ActionHandler implements ActionListener

public void actionPerformed(ActionEvent e) {// ***STATUS is a String declared at top of the program

if(e.getSource() == statusBox) status = (String)statusBox.getSelectedItem(); if(e.getSource() == genderBox) gender = (String)sexBox.getSelectedItem();

Code to get value selected from the combo boxes:e.getSource is a method that returns what component fired an event. In this case, user selected a combo box option

// This code displays a help message in a dialog box if help button pressed

// method e.getActionCommand knows what button is pressed

if(e.getActionCommand().equals("HELP")) JOptionPane.showMessageDialog (null, "Select R or S for status and then enter data in all the fields ", "INSTRUCTIONS", JOptionPane.PLAIN_MESSAGE );

A dialog box appears with this message on it

"Select R or S for status and then enter data in all the fields ",

The parameter “e” knows what component fired an event. In this case, it was the help button

Help Button Dialog Box displays help message to enter data in all the fields

// declare strings at top of actionperformed method for lastname,firstname etc. to store textfields inputif(e.getActionCommand().equals("ADD")) { // displays a dialog box telling user to enter data in all fields JOptionPane.showMessageDialog (null, "Be sure data in entered in all the fields", "INSTRUCTIONS", JOptionPane.PLAIN_MESSAGE ); if(status.equalsIgnoreCase("R")) // If user enter R for a regular client.. { // get the age from age textfield – only regular clients have ages age =Integer.parseInt( ageField.getText());

// get all the data from the other textfields using method // getText() as above, e.g. firstname = firstField.getText) // create a Client and add it to the ArrayList and the listModel

List.add(client); // Add client to ArrayList listModel.addElement(client); // If successful, add it to Jlist }// close if status else { // do same code for SeniorClient – no age needed} // close else firstField.setText(""); // Reset all textfields to a space } // close add

ADD button was pressed, this code follows help button code

ADD BUTTON PRESSED AND DIALOG BOX DISPLAYS“Be sure to enter data in all fields”

Reading in from a file

The input file looks like thisR (for regular clients ) firstname lastname  hobby, ..etc..... ageS(for Senior Clients firstname lastname hobby etc but no age

Algorithm:

1.You have to assemble the fields in the file into a Client  or Senior Client based on whether or not the input file entry has an age listed,  

2. create a new Client or SeniorClient with the input data

1.You then have to add the Client to the ArrayList ( declared at the top of the program)

2.and also add it to the listmodel ( also set up at the top of the constructor -- this represents the jLIST at the top of the GUI.

This part of reading in a client at a time  is very similar to the code you use to add a new Client in the listener.  except in the listener you will get the client data from the textfields.

// READ IN CLIENTS DATA FROM A FILE (SAMPLE ON MY SITE FOR // READ IN CLIENTS DATA FROM A FILE (SAMPLE ON MY SITE FOR PROJECT PROJECT public void BuildService() { Scanner scan = new Scanner(new File("Client1.txt")); Scanner linescan; try { // DECLARE ALL THE VARIABLES NEEDED E.G. LINE, SCAN Client client; // DECLARE CLIENT OBJECTS SeniorClient sclient;

while(scan.hasNext()) // CONTINUES UNTIL NO MORE LINES IN FILE { line = scan.nextLine(); // reads in a line from the file System.out.println(line); // for debugging purposes linescan = new Scanner(line); // create and another scanner for input line while (linescan.hasNext()) // while more fields to process { status = linescan.next(); // read in each field Firstname = linescan.next(); Lastname = linescan.next(); sex = linescan.next(); phone = linescan.next(); hobby = linescan.next();

if(status.charAt(0) == 'R')// if we have a regular Client , collect the value in the agefield { // gets the age using linescan.nextInt() // create the Client // Then add to ArrayList and the listModel ( the Jlist at the top of the GUI) } else { //create a SeniorClient using constructor of SeniorClient class // Then add to ArrayList and the listModel (the Jlist at the top of the //GUI)

} // add in the try/ catch clauses } } } }// close method