| | |
Listing questions
![]() |
•
•
Join Date: Sep 2008
Posts: 37
Reputation:
Solved Threads: 0
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
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();
}
}![]() |
Similar Threads
- Newbie questions: how to show a listing in pages (Perl)
- IIS Directory Listing Hiding Unauthorized Folders? (IT Professionals' Lounge)
- is it possible in php+html+js? (JavaScript / DHTML / AJAX)
- Best/worst interview questions? (IT Professionals' Lounge)
- Unanswered questions of mankind (Networking Hardware Configuration)
- Some general assembly questions (Assembly)
- Listing Integers in Numerical Order (C)
- About:Blank Homepage (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: Help with an error...
- Next Thread: Problem with remote applications interaction using URL
| Thread Tools | Search this Thread |
account android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer homework html ide image inheritance integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux loop method midlethttpconnection migrate mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program property ria scanner server set sharepoint smart sms smsspam sourcelabs splash sql sqlite subclass support swing testautomation textfield threads tree trolltech unlimited utility windows





