Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling a method of an object through a Vector
    primarykey
    data
    text
    <p>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.</p> <pre><code> 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); } } </code></pre> <p>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.</p> <h2>EDIT</h2> <p>From the CreatePanel class (initializing variables and storing the Flight object into the flightList Vector from JTextFields).</p> <pre><code>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 newFlight = 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); </code></pre> <p>From the Flight class:</p> <pre><code>public class Flight { // Flight constructor and all other variables/accessors/mutators are added here as well. private double price; public double getPrice() { return price; } </code></pre> <p>}</p> <h2>EDIT</h2> <p>Completed code:</p> <pre><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); } </code></pre> <p>All flightList Vectors have been updated with the element.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload