// TEST CODES FOR GUI DATA ENTRY AND VALIDATION /* An applet to enter interview details is required. The textfields and combo boxes must not be left blank. Swing components must be used. */ /* Compile this program using javac InterviewDetails.java command. Create an html file that will contain the applet tag to run the applet in the same directory as this program then run the html file. Make size of applet height 400 and width 600. */ // Import needed packages import java.awt.*; import java.awt.event.*; import javax.swing.*; // Make class a subclass of JApplet // Tell compiler the class will listen for events public class InterviewDetails extends JApplet implements ActionListener { // Application variables protected String candidateId, employeeId, interviewerName; protected String testDate, testScore, interviewDate; protected String comments, rating, status; // GUI variables like panel, layout, label, textfield, combobox protected JPanel panel; protected GridBagLayout gbl; protected GridBagConstraints gbc; protected JLabel labCandidateId, labEmployeeId, labInterviewerName; protected JLabel labTestDate, labTestScore, labInterviewDate; protected JLabel labComments, labRating, labStatus; protected JTextField txtCandidateId, txtEmployeeId, txtInterviewerName; protected JTextField txtTestDate, txtTestScore, txtInterviewDate, txtComments; protected JComboBox cmbRating, cmbStatus; final String ratings[] = {"1","2","3","4","5","6","7","8","9","10"}; final String statuses[] = {"Yes","No","Unknown"}; protected JButton butAccept, butCancel; public void init() // the init method { // Construct GUI using gridbag layout panel = new JPanel(); getContentPane().add(panel); gbl = new GridBagLayout(); panel.setLayout(gbl); gbc = new GridBagConstraints(); gbc.anchor = GridBagConstraints.NORTHWEST; // Align flush left gbc.gridx = 0; gbc.gridy = 0; labCandidateId = new JLabel("Candidate Id: "); gbl.setConstraints(labCandidateId, gbc); panel.add(labCandidateId); gbc.gridx = 1; gbc.gridy = 0; txtCandidateId = new JTextField(10); gbl.setConstraints(txtCandidateId, gbc); panel.add(txtCandidateId); gbc.gridx = 0; gbc.gridy = 1; labEmployeeId = new JLabel("Employee Id: "); gbl.setConstraints(labEmployeeId, gbc); panel.add(labEmployeeId); gbc.gridx = 1; gbc.gridy = 1; txtEmployeeId = new JTextField(10); gbl.setConstraints(txtEmployeeId, gbc); panel.add(txtEmployeeId); gbc.gridx = 0; gbc.gridy = 2; labInterviewerName = new JLabel("Interviewer Name: "); gbl.setConstraints(labInterviewerName, gbc); panel.add(labInterviewerName); gbc.gridx = 1; gbc.gridy = 2; txtInterviewerName = new JTextField(20); gbl.setConstraints(txtInterviewerName, gbc); panel.add(txtInterviewerName); gbc.gridx = 0; gbc.gridy = 3; labTestDate = new JLabel("Test Date: "); gbl.setConstraints(labTestDate, gbc); panel.add(labTestDate); gbc.gridx = 1; gbc.gridy = 3; txtTestDate = new JTextField(15); gbl.setConstraints(txtTestDate, gbc); panel.add(txtTestDate); gbc.gridx = 0; gbc.gridy = 4; labTestScore = new JLabel("Test Score: "); gbl.setConstraints(labTestScore, gbc); panel.add(labTestScore); gbc.gridx = 1; gbc.gridy = 4; txtTestScore = new JTextField(10); gbl.setConstraints(txtTestScore, gbc); panel.add(txtTestScore); gbc.gridx = 0; gbc.gridy = 5; labInterviewDate = new JLabel("Interview Date: "); gbl.setConstraints(labInterviewDate, gbc); panel.add(labInterviewDate); gbc.gridx = 1; gbc.gridy = 5; txtInterviewDate = new JTextField(15); gbl.setConstraints(txtInterviewDate, gbc); panel.add(txtInterviewDate); gbc.gridx = 0; gbc.gridy = 6; labComments = new JLabel("Comments: "); gbl.setConstraints(labComments, gbc); panel.add(labComments); gbc.gridx = 1; gbc.gridy = 6; txtComments = new JTextField(25); gbl.setConstraints(txtComments, gbc); panel.add(txtComments); gbc.gridx = 0; gbc.gridy = 7; labRating = new JLabel("Rating: "); gbl.setConstraints(labRating, gbc); panel.add(labRating); gbc.gridx = 1; gbc.gridy = 7; cmbRating = new JComboBox(ratings); cmbRating.setSelectedIndex( -1 ); // Default is blank gbl.setConstraints(cmbRating, gbc); panel.add(cmbRating); gbc.gridx = 0; gbc.gridy = 8; labStatus = new JLabel("Status: "); gbl.setConstraints(labStatus, gbc); panel.add(labStatus); gbc.gridx = 1; gbc.gridy = 8; cmbStatus = new JComboBox(statuses); cmbStatus.setSelectedIndex( -1 ); // Default blank gbl.setConstraints(cmbStatus, gbc); panel.add(cmbStatus); gbc.gridx = 3; gbc.gridy = 9; butAccept = new JButton("Accept"); gbl.setConstraints(butAccept, gbc ); panel.add(butAccept); butAccept.addActionListener(this); // Assign action listener gbc.gridx = 4; gbc.gridy = 9; butCancel = new JButton("Cancel"); gbl.setConstraints(butCancel, gbc); panel.add(butCancel ); butCancel.addActionListener( this ); // Assign action listener } // Method to show error message in status bar public void showError(String msg) { getAppletContext().showStatus(msg); } // Method to check if any field is empty public boolean hasNoEmptyFields() { String tmp; tmp = txtCandidateId.getText(); if(tmp.length() == 0) { showError("Candidate ID cannot be empty."); return false; } tmp = txtEmployeeId.getText(); if(tmp.length() == 0) { showError("Employee Id cannot be empty."); return false; } tmp = txtInterviewerName.getText(); if(tmp.length() == 0) { showError("Interviewer Name cannot be empty."); return false; } tmp = txtTestDate.getText(); if(tmp.length() == 0) { showError("Test Date cannot be empty."); return false; } tmp = txtTestScore.getText(); if(tmp.length() == 0) { showError("Test Score cannot be empty."); return false; } tmp = txtInterviewDate.getText(); if(tmp.length() == 0) { showError("Interview Date cannot be empty."); return false; } tmp = txtComments.getText(); if(tmp.length() == 0) { showError("Comments cannot be empty."); return false; } if(cmbRating.getSelectedIndex() == -1 ) { showError("A Rating must be selected." ); return false; } if(cmbStatus.getSelectedIndex() == -1 ) { showError("A Status must be selected." ); return false; } // Validation successful, no errors return true; } // Method to implement action listener public void actionPerformed( ActionEvent e ) { if(e.getSource() == butAccept) { if(hasNoEmptyFields()) // Call method to check empty fields { setDetails(); displayDetails(); } } else if(e.getSource() == butCancel) { getAppletContext().showStatus( "Cancel button pressed." ); } } // Method to accept data from GUI public void setDetails() { candidateId = txtCandidateId.getText(); employeeId = txtEmployeeId.getText(); interviewerName = txtInterviewerName.getText(); testDate = txtTestDate.getText(); testScore = txtTestScore.getText(); interviewDate = txtInterviewDate.getText(); comments = txtComments.getText(); rating = String.valueOf(cmbRating.getSelectedItem()); status = String.valueOf(cmbStatus.getSelectedItem()); } // Method to display data in command line public void displayDetails() { System.out.println("\nCandidate ID: " + candidateId); System.out.println("Employee Id: " + employeeId); System.out.println("Interviewer Name: " + interviewerName); System.out.println("Test Date: " + testDate); System.out.println("Test Score: " + testScore); System.out.println("Interview Date: " + interviewDate); System.out.println("Comments: " + comments); System.out.println("Rating: " + rating); System.out.println("Status: " + status); } } /* Note: Solution is based on the approach of the teacher which was different from that used in the textbook. */