CS 202 - Computer Science II - Fall 2005
Multiple Listeners
Loyola College >
Department of Computer Science >
Dr. James Glenn >
CS 202 >
Examples and Lecture Notes >
Multiple Listeners
FlightSearchApplet.java
/*
<APPLET CODE="FlightSearchApplet.class" WIDTH=400 HEIGHT=200></APPLET>
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
* A Java applet that has an interface that could be used for a flight search.
* This is the same as the earlier applet with the addition of two
* buttons for clear and swap.
*
* @author Jim Glenn
* @version 0.3 10/5/2005
*/
public class FlightSearchApplet extends JApplet
{
// these are declared as fields instead of local to the constructor
// so they can be used outside the constructor
private JTextField fromInput, toInput, resultOutput;
/**
* Creates the interface for this applet.
*/
public void init()
{
// use a border layout for the applet
getContentPane().setLayout(new BorderLayout());
// create the panel for the From and To inputs
JPanel routePanel = new JPanel();
routePanel.setLayout(new GridLayout(2, 2));
// create the label next to the "to" input area
JLabel fromLabel = new JLabel("From:");
routePanel.add(fromLabel);
// create the "to" input area
fromInput = new JTextField(10);
routePanel.add(fromInput);
// create the label and the input area for "from"
JLabel toLabel = new JLabel("To:");
routePanel.add(toLabel);
toInput = new JTextField(10);
routePanel.add(toInput);
// create the panel for the class dropdown and the non-stop checkbox
JPanel optionPanel = new JPanel(new GridLayout(2, 1));
// create the panel for the class drop-down
JPanel classPanel = new JPanel();
classPanel.add(new JLabel("Class:"));
// create an empty dropdown for class and put the 3 options in
JComboBox classBox = new JComboBox();
classBox.addItem("1st Class");
classBox.addItem("Business");
classBox.addItem("Coach");
classPanel.add(classBox);
optionPanel.add(classPanel);
// create the checkbox and add to panel
JCheckBox nonstop = new JCheckBox("Non-stop only");
optionPanel.add(nonstop);
// create a panel that holds everything but the search button and
// search results and put the panels in it
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.add(routePanel, BorderLayout.CENTER);
topPanel.add(optionPanel, BorderLayout.EAST);
// add the top panel to the top of the applet
getContentPane().add(topPanel, BorderLayout.NORTH);
// create a panel for the buttons (to protect them from stretching)
JPanel buttonPanel = new JPanel(new FlowLayout());
// create and add the buttons
JButton searchButton = new JButton("Search");
buttonPanel.add(searchButton);
JButton clearButton = new JButton("Clear");
buttonPanel.add(clearButton);
JButton swapButton = new JButton("Swap");
buttonPanel.add(swapButton);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
// create a text field to show results in and add it to the
// middle of the applet (but this is pretty ugly)
resultOutput = new JTextField();
resultOutput.setEditable(false);
getContentPane().add(resultOutput, BorderLayout.CENTER);
// create the listeners and connect them to the buttons
searchButton.addActionListener(new SearchListener());
ButtonListener l = new ButtonListener();
swapButton.addActionListener(l);
clearButton.addActionListener(l);
}
/**
* An inner class for the search button's listener.
*/
private class SearchListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String from = fromInput.getText();
String to = toInput.getText();
resultOutput.setText("Searching for flights from " + from + " to " + to + "...failed to connect to database");
}
}
/**
* An inner class for the other buttons' listeners. This could be done
* with two separate classes, but is done with one here to illustrate
* how actionPerformed can determine which button sent the event.
*/
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// determine which button sent the event --
// the action command is the text on the button by default
if (e.getActionCommand().equals("Clear"))
{
fromInput.setText("");
toInput.setText("");
resultOutput.setText("");
}
else
{
String temp = fromInput.getText();
fromInput.setText(toInput.getText());
toInput.setText(temp);
resultOutput.setText("");
}
}
}
}
This code can also be downloaded from the file
FlightSearchApplet.java.