I have the following listing application that allows the user to make selections to build a skateboard. How can I add the values to the arrays which are declared? I do not understand how each value will be displayed when a particular option is selected.
Also... I want to add a button to that displays a message showing the total and tax when the user has selected all of their options... I am not sure where to add that code....
here is what i have so far.... ignore the comments they are from a similar listing exercise which I have edited, and just not deleted yet
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.*;
import javax.swing.*;
/**
* This class demonstrates the List Component.
*/
public class SkateBoard extends JFrame
{
private JPanel deckPanel; // To hold components
private JPanel selectedDeckPanel; // To hold components
private JList deckList; // A list of decks
private JTextField selectedDeck; // The selected deck
private JLabel deckLabel; // To display a message
private JPanel truckPanel; // To hold components
private JPanel selectedTruckPanel; // To hold components
private JList truckList; // A list of trucks
private JTextField selectedTruck; // The selected truck
private JLabel truckLabel; // To display a message
private JPanel wheelPanel; // To hold components
private JPanel selectedWheelPanel; // To hold components
private JList wheelList; // A list of wheels
private JTextField selectedWheel; // The selected wheel
private JLabel wheelLabel; // To display a message
private JPanel extraPanel; // To hold components
private JPanel selectedExtraPanel; // To hold components
private JList extraList; // A list of wheels
private JTextField selectedExtra; // The selected wheel
private JLabel extraLabel;
private JButton button;
private JPanel buttonPanel;
// The following array holds the values that will be
// displayed in the deckList list component.
private String[] decks = {"The Master Thrasher", "The Dictator", "The Street King"};
private String[] trucks = { "7.75 inch axle", "8-inch axle", "8.5 inch axle"};
private String[] wheels = { "51 mm", "55 mm", "58 mm", "61 mm"};
private String[] extras = { "Grip Tape", "Bearings", "Riser Pads", "Nuts and Bolts Kit"};
/**
* Constructor
*/
public SkateBoard()
{
// Call the JFrame constructor.
super("Build Your Custom SkateBoard");
setSize(200,200);
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a BorderLayout manager for the content pane.
setLayout(new GridLayout(2,4));
// Build the deck and selectedDeck panels
buildDeckPanel();
buildTruckPanel();
buildWheelPanel();
buildExtraPanel();
buildSelectedDeckPanel();
buildSelectedTruckPanel();
buildSelectedWheelPanel();
buildSelectedExtraPanel();
// Add the panels to the content pane.
add(deckPanel);
add(truckPanel);
add(wheelPanel);
add(extraPanel);
add(selectedDeckPanel);
add(selectedTruckPanel);
add(selectedWheelPanel);
add(selectedExtraPanel);
// Pack and display the window.
pack();
setVisible(true);
}
/**
* The buildDeckPanel method adds a list containing
* the names of the decks to a panel.
*/
private void buildDeckPanel()
{
// Create a panel to hold the list.
deckPanel = new JPanel();
// Create the list.
deckList = new JList(decks);
// Set the selection mode to single selection.
deckList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Register the list selection listener.
deckList.addListSelectionListener(new DeckListener());
// Add the list to the panel.
deckPanel.add(deckList);
}
private void buildTruckPanel()
{
// Create a panel to hold the list.
truckPanel = new JPanel();
// Create the list.
truckList = new JList(trucks);
// Set the selection mode to single selection.
truckList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Register the list selection listener.
truckList.addListSelectionListener(new TruckListener());
// Add the list to the panel.
truckPanel.add(truckList);
}
private void buildWheelPanel()
{
// Create a panel to hold the list.
wheelPanel = new JPanel();
// Create the list.
wheelList = new JList(wheels);
// Set the selection mode to single selection.
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);
}
/**
* The buildSelectedDeckPanel method adds an uneditable
* text field to a panel.
*/
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 class DeckListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
// Get the selected string from the list.
String selection = (String) deckList.getSelectedValue();
// Store the selected string in the text field.
selectedDeck.setText(selection);
}
}
private class TruckListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
// Get the selected string from the list.
String selection = (String) truckList.getSelectedValue();
// Store the selected string in the text field.
selectedTruck.setText(selection);
}
}
private class WheelListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
// Get the selected string from the list.
String selection = (String) wheelList.getSelectedValue();
// Store the selected string in the text field.
selectedWheel.setText(selection);
}
}
private class ExtraListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
// Get the selected string from the list.
String selection = (String) extraList.getSelectedValue();
// Store the selected string in the text field.
selectedExtra.setText(selection);
}
}
/**
* The main method creates an instance of the SkateBoard
* class, which causes it to display its window.
*/
public static void main(String[] args)
{
new SkateBoard();
}
}