I have a GUI app which in which I need to pass a value from an array to a List... here is what I have... The problem occurs when I try to pass the array element to the ActionListener...

im not sure exactly how to do it...

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.*;
import javax.swing.*;

public class SkateBoard extends JFrame
{
   private JPanel deckPanel;        
   private JPanel selectedDeckPanel;
   private JList deckList;         
   private JTextField selectedDeck;  
   private JLabel deckLabel;              
   
   private JPanel truckPanel;      
   private JPanel selectedTruckPanel; 
   private JList truckList;          
   private JTextField selectedTruck; 
   private JLabel truckLabel;             
   
   private JPanel wheelPanel;       
   private JPanel selectedWheelPanel; 
   private JList wheelList;           
   private JTextField selectedWheel;  
   private JLabel wheelLabel;         
   
   private JPanel extraPanel;        
   private JPanel selectedExtraPanel;
   private JList extraList;        
   private JTextField selectedExtra;  
   private JLabel extraLabel; 
   public double deckPrice;
   public double truckPrice;
   public double wheelPrice;
   public double extraPrice;
   private JButton button;
   private JPanel buttonPanel;
  
   
   private JTextField total;

   public String[] decks = {"The Master Thrasher", "The Dictator", "The Street King"};
   public String[] trucks = { "7.75 inch axle", "8-inch axle", "8.5 inch axle"};
   public String[] wheels = { "51 mm", "55 mm", "58 mm", "61 mm"};
   public String[] extras = { "Grip Tape", "Bearings", "Riser Pads", "Nuts and Bolts Kit"};
 
    private JLabel totalLabel;
    private JButton calcButton;
   
    public double totalPrice = deckPrice + truckPrice + wheelPrice + extraPrice;
    
   public SkateBoard()
   {

      super("Build Your Custom SkateBoard");
      
      setSize(200,200);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      
      setLayout(new GridLayout(3,4));

      buildDeckPanel();
      buildTruckPanel();
      buildWheelPanel();
      buildExtraPanel();
      buildSelectedDeckPanel();
      buildSelectedTruckPanel();
      buildSelectedWheelPanel();
      buildSelectedExtraPanel();
      buildButtonPanel();
      
      add(deckPanel);
      add(truckPanel);
      add(wheelPanel);
      add(extraPanel);
      add(selectedDeckPanel);
      add(selectedTruckPanel);
      add(selectedWheelPanel);
      add(selectedExtraPanel);
      add(buttonPanel);

      pack();
      setVisible(true);
   
      
     
   }
   
    public double getTotal()
      {
        double totalPrice;
        
        totalPrice = deckPrice + truckPrice + wheelPrice + extraPrice;
        
        
        
          return totalPrice;
      }
   

    
   private void buildDeckPanel()
   {
     
      deckPanel = new JPanel();

     
      deckList = new JList(decks);

     
      deckList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

      deckList.addListSelectionListener(new DeckListener());

  
      deckPanel.add(deckList);
   }
   private void buildTruckPanel()
   {
   
      truckPanel = new JPanel();

    
      truckList = new JList(trucks);

     
      truckList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

      
      truckList.addListSelectionListener(new TruckListener());

     
      truckPanel.add(truckList);
   }

   private void buildWheelPanel()
   {
     
      wheelPanel = new JPanel();

      
      wheelList = new JList(wheels);

      .
      wheelList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

      // Register the list selection listener.
      wheelList.addListSelectionListener(new WheelListener());

      // Add the list to the panel.
      wheelPanel.add(wheelList);
   }
   
    private void buildExtraPanel()
   {
      // Create a panel to hold the list.
      extraPanel = new JPanel();

      // Create the list.
      extraList = new JList(extras);

      // Set the selection mode to single selection.
      extraList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

      // Register the list selection listener.
      extraList.addListSelectionListener(new ExtraListener());

      // Add the list to the panel.
      extraPanel.add(extraList);
   }
    
    

   
   private void buildSelectedDeckPanel()
   {
      // Create a panel to hold the text field.
      selectedDeckPanel = new JPanel();

      // Create the label.
      deckLabel = new JLabel("Deck: ");

      // Create the text field.
      selectedDeck = new JTextField(12);

      // Make the text field uneditable.
      selectedDeck.setEditable(false);

      // Add the label and text field to the panel.
      selectedDeckPanel.add(deckLabel);
      selectedDeckPanel.add(selectedDeck);
   }
   
      private void buildSelectedTruckPanel()
   {
      // Create a panel to hold the text field.
      selectedTruckPanel = new JPanel();

      // Create the label.
      truckLabel = new JLabel("Truck: ");

      // Create the text field.
      selectedTruck = new JTextField(10);

      // Make the text field uneditable.
      selectedTruck.setEditable(false);

      // Add the label and text field to the panel.
      selectedTruckPanel.add(truckLabel);
      selectedTruckPanel.add(selectedTruck);
   }

private void buildSelectedWheelPanel()
   {
      // Create a panel to hold the text field.
      selectedWheelPanel = new JPanel();

      // Create the label.
      wheelLabel = new JLabel("Wheel: ");

      // Create the text field.
      selectedWheel = new JTextField(5);

      // Make the text field uneditable.
      selectedWheel.setEditable(false);

      // Add the label and text field to the panel.
      selectedWheelPanel.add(wheelLabel);
      selectedWheelPanel.add(selectedWheel);
   }

private void buildSelectedExtraPanel()
   {
      // Create a panel to hold the text field.
      selectedExtraPanel = new JPanel();

      // Create the label.
      extraLabel = new JLabel("Extras: ");

      // Create the text field.
      selectedExtra = new JTextField(15);

      // Make the text field uneditable.
      selectedExtra.setEditable(false);

      // Add the label and text field to the panel.
      selectedExtraPanel.add(extraLabel);
      selectedExtraPanel.add(selectedExtra);
   }

private void buildButtonPanel()
    {
    buttonPanel = new JPanel();
    calcButton = new JButton("Calculate");
    calcButton.addActionListener(new CalcButtonListener());
    buttonPanel.add(calcButton);  
    }

public class CalcButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {   
        
        
        JOptionPane.showMessageDialog(null, "Subtotal: $" + 
                totalPrice);
    }
}
   public class DeckListener implements ListSelectionListener
   {
      public void valueChanged(ListSelectionEvent e)
      {
         // Get the selected string from the list.
          
         
         String selection = (String) deckList.getSelectedValue();
         
          if (selection == decks[0])
         deckPrice = 60;
         if (selection == decks[1])
             deckPrice = 45;
         if (selection == decks[2])
             deckPrice = 50;
       selectedDeck.setText(selection);
             
      }
      
   }
   
   public class TruckListener implements ListSelectionListener
   {
      public void valueChanged(ListSelectionEvent e)
      {
         // Get the selected string from the list.
         String selection = (String) truckList.getSelectedValue();
          
         
           if (selection == trucks[0])
         truckPrice = 35;
         if (selection == trucks[1])
             truckPrice = 40;
         if (selection == trucks[2])
             truckPrice = 45;
         
         // Store the selected string in the text field.
         selectedTruck.setText(selection);
      }
      
   }
   
     public class WheelListener implements ListSelectionListener
   {
      public void valueChanged(ListSelectionEvent e)
      {
         // Get the selected string from the list.
         String selection = (String) wheelList.getSelectedValue();
            
           if (wheels[0].isSelected)
         wheelPrice = 60;
         if (selection == wheels[1])
             wheelPrice = 45;
         if (selection == wheels[2])
             wheelPrice = 50;
         // Store the selected string in the text field.
         selectedWheel.setText(selection);
      }
      
    
   }
     
      public class ExtraListener implements ListSelectionListener
   {
        
      public void valueChanged(ListSelectionEvent e)
      {
         // Get the selected string from the list.
         String selection = (String) extraList.getSelectedValue();
          
           if (selection == extras[0])
         extraPrice = 60;
         if (selection == extras[1])
             extraPrice = 45;
         if (selection == extras[2])
             extraPrice = 50;
         // Store the selected string in the text field.
         selectedExtra.setText(selection);
      }
      
    
   }
       

   public static void main(String[] args)
   {
      new SkateBoard();
      
      }
     
   }

is the error on f(wheels[0].isSelcted)?

is the error on getting the total?

its on gettting the total even though that line was wrong ask well..

i am getting warnings in the

(selection == decks[0])
deckPrice = 60;
if (selection == decks[1])
deckPrice = 45;
if (selection == decks[2])
deckPrice = 50;

lines but do not know another way to take the selected items and assign them a price...

You don't need the getTotal section. I deleted it and rewrote the Calculate action like this and it returned the Total, Subtotal and Tax amounts...

public class CalcButtonListener implements ActionListener
{

      public void actionPerformed(ActionEvent e)
      {
         double subtotal;  // The order subtotal
         double tax;       // The amount of sales tax
         double total;     // The order total

         // Calculate the subtotal.
         subtotal = deckPrice + truckPrice + wheelPrice + extraPrice;

         // Calculate the sales tax.
         tax = subtotal * TAX_RATE;

         // Calculate the total.
         total = subtotal + tax;

         // Create a DecimalFormat object to format
         // the total as a dollar amount.
         DecimalFormat dollar = new DecimalFormat("0.00");

         // Display the charges.
         JOptionPane.showMessageDialog(null, "Subtotal: $" +
                      dollar.format(subtotal) + "\n" +
                      "Tax: $" + dollar.format(tax) + "\n" +
                      "Total: $" + dollar.format(total));
      }
   }

Also, you need to fix your wheel listener like so:

public class WheelListener implements ListSelectionListener
   {
      public void valueChanged(ListSelectionEvent e)
      {
         // Get the selected string from the list.
         String selection = (String) wheelList.getSelectedValue();

         if ( selection == wheels[0])
             wheelPrice = 60;
         if (selection == wheels[1])
             wheelPrice = 45;
         if (selection == wheels[2])
             wheelPrice = 50;
         // Store the selected string in the text field.
         selectedWheel.setText(selection);
      }


   }

Also, you might want to remove the period from this section in the BuildWheelsPanel:

 wheelList = new JList(wheels);

  .
  wheelList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

That should do it for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.