samedi 30 avril 2016

java - add in checkbox

I am creating a pizza ordering system. As of now I had a stroller in order to select the toppings but I want to change to check boxes so I can elect multiple values at once without using ctrl. The problem is though I really have no idea how to use them. I never had to use them before. Can someone please show me how it is done. Thank you here is the code:

 package loan;

import javafx.application.Application;
import javafx.collections.*;
    import javafx.event.*;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
    import javafx.stage.Stage;
    import javafx.scene.layout.*;
    import javafx.scene.paint.Color;
    import javafx.scene.control.*;
    import javafx.scene.Scene;
    import javafx.scene.shape.*;
    import javafx.scene.paint.*;

    public class pizzas extends Application{
        private ComboBox<String> size;
        private ListView<String> toppings;
        private TextField order;
        private Button orderit, clearit;
        private Label lb_size, lb_tops, lb_order;
        private ObservableList<String> flavor =
                FXCollections.observableArrayList (
                        "Small", "Medium",
                        "Large", "extra Large");
        private ObservableList<String> tps =
                FXCollections.observableArrayList(
                        "pineapples", "pepperoni",
                        "peppers", "bacon", "sausage",
                        "ham");
        public void start(Stage primaryStage) {
            //areas to place the various components
            VBox pane = new VBox(15);
            HBox flavor_pane = new HBox(10);
            HBox topping_pane = new HBox(10);
            HBox order_pane = new HBox(10);

            lb_size = new Label("Sizes");
            size = new ComboBox(flavor);
            size.setVisibleRowCount(4);
            size.setValue(flavor.get(0)); // display the first one

            flavor_pane.getChildren().addAll(lb_size, size);

            //lb_tops = new Label("toppings");
            //toppings = new ListView(tps);
            //toppings.setPrefSize(100,80);
    //toppings.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

               //topping_pane.getChildren().addAll(lb_tops, toppings);

            lb_order = new Label("Order Summary");
            order = new TextField();
            order.setEditable(false);
            order.setPrefColumnCount(25);

            order_pane.getChildren().addAll(lb_order, order);

            orderit = new Button("Place Order");
            clearit = new Button("Clear Order");

            // Subscribe for when the user clicks the buttons
            OrderHandler oh = new OrderHandler();
            orderit.setOnAction(oh);
            clearit.setOnAction(oh);

            pane.getChildren().addAll(flavor_pane,
                    topping_pane, order_pane, orderit, clearit);

            Scene scene = new Scene(pane, 400, 300);
            primaryStage.setTitle("pizza ordering");
            primaryStage.setScene(scene);
            primaryStage.show();

        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Application.launch(args);
        }
    class OrderHandler implements EventHandler<ActionEvent>{
        public void handle(ActionEvent e) {
            // was it the clear button  
            if (e.getSource() == clearit) {
                order.setText("");
                toppings.getSelectionModel().clearSelection();
                size.setValue(flavor.get(0));
                System.out.println("the order has been cancelled. No order will be made.");
                return;
            }
            // flavor
            String result = size.getValue();
            // for toppings
            ObservableList<String> selections =
            toppings.getSelectionModel().getSelectedItems();
            // convert to an array
            Object[] objs = selections.toArray();
            for (int k =0 ; k < objs.length; k++){
                result += " " + objs[k].toString();
            }
            order.setText(result);
        }
    }
    }

As of now. I commented out the lines that created the toppings stroller.When I start the program I select the size and once i press order it appears in the order summary.




Aucun commentaire:

Enregistrer un commentaire