943,748 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1187
  • Java RSS
Nov 19th, 2008
0

Passing Array element to a list

Expand Post »
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();
      
      }
     
   }
Similar Threads
Reputation Points: 4
Solved Threads: 0
Light Poster
cproud21 is offline Offline
42 posts
since Sep 2008
Nov 19th, 2008
0

Re: Passing Array element to a list

is the error on f(wheels[0].isSelcted)?
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Nov 19th, 2008
0

Re: Passing Array element to a list

is the error on getting the total?
Last edited by christiangirl; Nov 19th, 2008 at 3:28 am.
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Nov 19th, 2008
0

Re: Passing Array element to a list

its on gettting the total even though that line was wrong ask well..
Reputation Points: 4
Solved Threads: 0
Light Poster
cproud21 is offline Offline
42 posts
since Sep 2008
Nov 19th, 2008
0

Re: Passing Array element to a list

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...
Reputation Points: 4
Solved Threads: 0
Light Poster
cproud21 is offline Offline
42 posts
since Sep 2008
Nov 19th, 2008
0

Re: Passing Array element to a list

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LDeArm is offline Offline
1 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Time Calculator
Next Thread in Java Forum Timeline: Execute an sql script with routines in jdbc..?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC