JavaFX - How do I check if the user selected multiple items in a ListView

Viewed 29

I'm trying to have a user pick as many items on a JavaFX ListView. However, the method I'm using doesn't seem to take into account the the possibility of multiple choices.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.Alert.AlertType;

/**
 *  Skateboard Designer
 *  
 *  @author Emanuel "Manny" Luna
 */

public class SkateboardDesigner_Luna extends Application
{
    public static void main(String[] args)
    {
        // Launch the application.
        launch(args);
    }

    @Override
    public void start(Stage primaryStage)
    {
        // *********************************************
        // Set up components.
        // *********************************************      

        // Build the Decks ComboBox.
        ComboBox<String> deckComboBox = new ComboBox<>();
        deckComboBox.getItems().addAll("The Master Thrasher $60",
                "The Dictator        $45",
                "The Street King     $50");

        // Label to prompt the user to select a deck
        Label deckPromptLabel = new Label("Select a Deck");
        VBox deckVBox = new VBox(10, deckPromptLabel, deckComboBox);

        // Build the Truck Assemblies ComboBox.
        ComboBox<String> truckComboBox = new ComboBox<>();
        truckComboBox.getItems().addAll("7.75 inch axle  $35",
                "8 inch axle     $40",
                "8.5 inch axle   $45");

        // Label to prompt the user to select a truck assembly
        Label truckPromptLabel = new Label("Select a Truck Assembly");
        VBox truckVBox = new VBox(10, truckPromptLabel, truckComboBox);

        // Build the Wheels ComboBox.
        ComboBox<String> wheelComboBox = new ComboBox<>();
        wheelComboBox.getItems().addAll("51mm  $20",
                "55mm  $22",
                "58mm  $24",
                "61mm  $28");

        // Label to prompt the user to select wheels
        Label wheelPromptLabel = new Label("Select Wheels");
        VBox wheelVBox = new VBox(10, wheelPromptLabel, wheelComboBox);

        // Build the Misc ListView.
        ListView<String> miscListView = new ListView<>();
        miscListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        miscListView.getItems().addAll("Grip Tape        $10",
                "Bearings         $30",
                "Riser Pads       $2",
                "Nuts & Bolts Kit $3");

        // Label to prompt the user to select misc parts
        Label miscPromptLabel = new Label("Select Misc Parts");
        VBox miscVBox = new VBox(10, miscPromptLabel, miscListView);
        HBox partsHBox = new HBox(10, deckVBox, truckVBox, wheelVBox, miscVBox);

        // Create the output label for total cost.
        Label costDescriptor = new Label("Cost:");
        Label costOutputLabel = new Label("0.00");
        HBox costHBox = new HBox(10, costDescriptor, costOutputLabel);
        costHBox.setAlignment(Pos.CENTER);

        // Create the Calculate button.
        Button calcButton = new Button("Calculate Cost");

        // Put everything into a VBox
        VBox mainVBox = new VBox(10, partsHBox, costHBox, calcButton);
        mainVBox.setAlignment(Pos.CENTER);
        mainVBox.setPadding(new Insets(10));

        // *********************************************
        // Register the event handler for the button here.
        // *********************************************   
        calcButton.setOnAction(new EventHandler<ActionEvent>()
        {

            @Override
            public void handle(ActionEvent arg0) 
            {

                try {

                    double subTotal = 0;
                    double taxes = 0;
                    double total = 0;

                    // First, the function will check to see what the 
                    // user has chosen in the combo box's.

                    String deck = deckComboBox.getSelectionModel().getSelectedItem();
                    String truck = truckComboBox.getSelectionModel().getSelectedItem();
                    String wheels = wheelComboBox.getSelectionModel().getSelectedItem();
                    String misc = miscListView.getSelectionModel().getSelectedItem();


                    // Second, the function checks what has been selected


                    if (deck.equals("The Master Thrasher $60"))
                    {
                        subTotal += 60;
                    }

                    else if (deck.equals("The Dictator        $45"))
                    {
                        subTotal += 45;
                    }

                    else if (deck.equals("The Street King     $50"))
                    {
                        subTotal += 50;
                    }



                    if (truck.equals("7.75 inch axle  $35"))
                    {
                        subTotal += 35;

                    }

                    else if (truck.equals("8 inch axle     $40"))
                    {
                        subTotal += 40;
                    }

                    else if (truck.equals("8.5 inch axle   $45"))
                    {
                        subTotal += 45;
                    }


                    if (wheels.equals("51mm  $20"))
                    {
                        subTotal += 20;
                    }

                    else if (wheels.equals("58mm  $24"))
                    {
                        subTotal += 24;
                    }

                    else if (wheels.equals("61mm  $28"))
                    {
                        subTotal += 28;
                    }

// Here's the mistake
                    if(misc.equals(""))
                    {
                        subTotal += 10;
                    }

                    if (misc.equals("Bearings         $30"))
                    {
                        subTotal += 30;
                    }

                    if (misc.equals("Riser Pads       $2"))
                    {
                        subTotal += 2;
                    }

                    if (misc.equals("Nuts & Bolts Kit $3"))
                    {
                        subTotal += 3;
                    }

                        
                    
                    
                    
                    taxes = subTotal * 0.07;
                    
                    total = subTotal + taxes;
                    
                    costDescriptor.setText(String.format("Sub Total:\nTax:\nTotal:"));
                    costOutputLabel.setText(String.format("$%.2f%n$%.2f%n$%.2f", subTotal, taxes, total));

                }


                catch(Exception e)
                {
                    Alert a = new Alert(AlertType.ERROR, "Please pick one item from each category.");
                    a.setTitle("Error Message");
                    a.show();
                }


            }

        });

        // *********************************************
        // Show the GUI!
        // *********************************************

        // Add the main VBox to a scene.
        Scene scene = new Scene(mainVBox);

        // Set the scene to the stage and display it.
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

When using "misc" it only takes the first option the user presses. How can I have it that it can add multiple items in the ListView and the method to know that the user checked a couple of items and then add the prices?

1 Answers
Related