I am creating a pizza ordering system that when it runs u have to select the size from a scroll bar and then the toppings from a scroll bar. then once order is pressed. what is ordered will appear in order summary. I never use check boxes so I have no idea how to replace the stroller for toppings for check boxes. Can someone please help me. I need this done. The reason I need to is so I can select multiple values and then deselect when I click again. Thank you
here is my code:
package lab;
import javafx.application.Application;
import javafx.collections.*;
import javafx.event.*;
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 pizza extends Application{
private ComboBox<String> size;
private ListView<String> toppings;
private TextField order;
private Button orderit, clearit;
private Label lb_flavor, 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_flavor = new Label("Sizes");
size = new ComboBox(flavor);
size.setVisibleRowCount(4);
size.setValue(flavor.get(0)); // display the first one
flavor_pane.getChildren().addAll(lb_flavor, 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));
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);
}
}
}
Aucun commentaire:
Enregistrer un commentaire