Add functionality to search for an Itinerary. Create labels and text fields that
ID: 3817641 • Letter: A
Question
Add functionality to search for an Itinerary. Create labels and text fields that allow the user to enter a bus type, a source bus station, and a destination bus station, as well as the departure time and the arrival time to search for one or more available bus routes in an Itinerary. Also, create a ComboBox (see Chapter 11) that will allow the user to select from a list of bus route Itineraries. Each item in the ComboBox represents an Itinerary element, e.g. the first comboBox element is the first Itinerary in the array. [15 pts] Methods: Your BusFrame class should include the following methods (The methods that check for validation must have message dialogs indicating to the user if the information entered in each text field is invalid): initComponents() : Method used to initialize UI components and controls default constructor : Inside the default constructor, call the initComponents() method convertTime(String input) : converts time string (from text input -- ie departure time text field) to a Time object and returns the Time object checkValidBusType() : returns true if entered bus type is valid and exists in BusType enum and can be converted to a BusType object checkValidBusStations(String src, String dest) : will return true if both the source and destination bus stations entered in the text fields exist in BusStation enum checkValidTime(String depTime, String arriveTime) : checks if the time is in valid format (hh:mm a) and returns true if it is in the correct format Make sure all text fields have error checking and exception handling for invalid input. For example, if the user enters an integer as a departure time instead of a String (ie 1200 instead of 12:00 PM), a JOptionPane error message should appear stating, "Incorrect format for departure time." If the bus type they entered is not in the BusType enum, then the option pane should say, "Bus type unavailable. Please choose a different bus type." If the bus station they entered is not in the BusStation enum, then the option pane should say, "Unknown city." Make sure the times are in hh:mm a format. [8 pts]
Explanation / Answer
package com.chegg;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SwingTests extends JPanel {
private static JComboBox comboBox;
private static JTextField textField;
// Create a form with the fields
public SwingTests() {
super(new BorderLayout());
JPanel labelPanel = new JPanel(new GridLayout(2, 1)); // 2 rows 1 column
add(labelPanel, BorderLayout.WEST);
JPanel fieldPanel = new JPanel(new GridLayout(2, 1)); // 2 rows 1 column
add(fieldPanel, BorderLayout.CENTER);
JLabel labelCombo = new JLabel("Bank Code");
String[] options = { "test1", "test 2", "test 3", "test 4", "test 5" };
comboBox = new JComboBox(options);
comboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
JLabel labelTextField = new JLabel("Enter Name ");
textField = new JTextField();
// Add labels
labelPanel.add(labelCombo);
labelPanel.add(labelTextField);
fieldPanel.add(comboBox);
fieldPanel.add(textField);
}
public static void main(String[] args) {
final SwingTests swingTests = new SwingTests();
JButton submit = new JButton("Submit Form");
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createIban((String) comboBox.getSelectedItem(),
textField.getText());
}
});
JFrame f = new JFrame("Text Form Example");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(swingTests, BorderLayout.NORTH);
JPanel p = new JPanel();
p.add(submit);
f.getContentPane().add(p, BorderLayout.SOUTH);
// Show the frame
f.pack();
f.setVisible(true);
}
private static void createIban(String selectedItem, String text) {
// Do stuff with your data
System.out.println("Im in createIban with the values: " + selectedItem
+ " and " + text);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.