This is a part of a larger program that I'm doing where the user can create a Flight object by entering data into JTextFields. The data is stored into a Vector, called flightList. On a second panel of my JApplet, the user can use a JComboBox - flightBox - to select one of the Flights that they have created. When the Flight is selected from the JComboBox, the getPrice() method needs to be called for the selected Flight object, and displayed in a JLabel below.
private class ChoiceListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//needs to be completed
if (flightBox.getSelectedIndex() == 0)
{
//flightBox.getSelectedItem(); // returns selected object
outro.setText("The price for your flight is:");
int p = flightBox.getSelectedIndex();
Flight selectedFlight = flightList.get(p);
String selectedPrice = money.format(selectedFlight.getPrice()) + "";
fPrice.setText(selectedPrice);
}
}
I thought I was on the right track, and I've tried a lot of different variations but none seem to be working. Also, I know that the Flights are being added to flightList, because the JComboBox does display all added Flights. I've got all the labels set up correctly, I think. I just need to figure out how to actually get the selected Flight object from flightList using the flightBox, and pull that price value from it using the getPrice method.
EDIT
From the CreatePanel class (initializing variables and storing the Flight object into the flightList Vector from JTextFields).
CityTime departure = new CityTime();
departure.setCity(dC);
departure.setDate(dD);
departure.setTime(dT);
CityTime arrival = new CityTime();
arrival.setCity(aC);
arrival.setDate(aD);
arrival.setTime(aT);
Flight newF开发者_高级运维light = new Flight();
newFlight.setAirlines(air);
newFlight.setFlightNum(iNum = Integer.parseInt(num));
newFlight.setPrice(dPrc = Double.parseDouble(prc));
newFlight.setDeparture(dC, dD, dT);
newFlight.setArrival(aC, aD, aT);
flightList.add(newFlight);
From the Flight class:
public class Flight
{
// Flight constructor and all other variables/accessors/mutators are added here as well.
private double price;
public double getPrice()
{
return price;
}
}
EDIT
Completed code:
if (flightBox.getSelectedIndex() != -1)
{
//flightBox.getSelectedItem(); // returns selected object
outro.setText("The price for your flight is:");
int p = flightBox.getSelectedIndex();
Flight selectedFlight = flightList.get(p);
String selectedPrice = money.format(selectedFlight.getPrice()) + "";
fPrice.setText(selectedPrice);
}
All flightList Vectors have been updated with the element.
精彩评论