选择的JavaFX组合框项目显示在列表框

时间:2016-05-11 02:34:55

标签: javafx

我在传输组合框中选择的内容并在列表框中显示时遇到了一些麻烦。我不确定该怎么做。我尝试了许多不同的方法,没有运气。互联网上给出的所有示例均指场景构建器。我不能用场景构建器创建它。有人可以帮我一把,帮助我。我不确定它应该是一个动作事件还是听众来做我想做的事情。请原谅新问题。谢谢!!

package application;

import java.util.List;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    // Pane pane = new Pane();

    double beverageArray[] = { 1.95, 1.50, 1.25, 2.95, 2.50, 1.50 };
    double appetizerArray[] = { 5.95, 6.95, 8.95, 8.95, 10.95, 12.95, 6.95 };
    double mainCourseArray[] = { 15.95, 13.95, 13.95, 11.95, 19.95, 20.95, 18.95, 13.95, 14.95 };
    double desertArray[] = { 5.95, 3.95, 5.95, 4.95, 5.95 };
    String[] beverages = { "Soda", "Tea", "Coffee", "Mineral Water", "Juice", "Milk" };
    double totalCost = 0;

    GridPane pane = new GridPane();
    pane.setAlignment(Pos.TOP_LEFT);
    pane.setPadding(new Insets(20, 20, 20, 20));
    pane.setHgap(5.5);
    pane.setVgap(5.5);

    ComboBox<String> beverageComboBox = new ComboBox<String>();
    ObservableList<String> items = FXCollections.observableArrayList(beverages);
    beverageComboBox.getItems().addAll(items);

    // ----------------------------------------------------------------------------------------------------------
    // final ComboBox<String> beverageComboBox = new ComboBox<String>();
    // beverageComboBox.getItems().addAll("Soda", "Tea", "Coffee", "Mineral
    // Water", "Juice",
    // "Milk");
    // ----------------------------------------------------------------------------------------------------------

    final ComboBox<String> appetizerComboBox = new ComboBox<String>();
    appetizerComboBox.getItems().addAll("Buffalo Wings", "Buffalo Fingers", "Potato Skins", "Nachos",
            "Mushroom Caps", "Shrimp Cocktail");

    final ComboBox<String> maincourseComboBox = new ComboBox<String>();
    maincourseComboBox.getItems().addAll("Seafood Alfredo", "Chicken Alfredo", "Chicken Picatta", "Turkey Club",
            "Lobster Pie", "Prime Rib", "Shrimp Scampi", "Turkey Dinner", "Stuffed Chicken");

    final ComboBox<String> dessertComboBox = new ComboBox<String>();
    dessertComboBox.getItems().addAll("Apple Pie", "Sundae", "Carrot Cake", "Mud Pie", "Apple Crisp");
    /*
     * beverageComboBox.setValue("Soda"); appetizerComboB(ox.setValue(
     * "Buffalo Wings"); maincourseComboBox.setValue("Seafood Alfredo");
     * dessertComboBox.setValue("Apple Pie");
     */

    Image image = new Image("fd.jpg");
    pane.getChildren().add(new ImageView(image));
    // -----------------------------------------------------------------------------------------------------------
    Label tit = new Label("John's");
    tit.setFont(Font.font("Tahoma", FontWeight.BOLD, 32));
    pane.add(tit, 1, 0);
    // ---------------------------------------------------------------------------------------------------------------
    Label wi = new Label("Waiter Information");
    wi.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    pane.add(wi, 0, 5);

    Label tn = new Label("Table Number:");
    pane.add(tn, 0, 6);
    TextField tnf = new TextField();
    pane.add(tnf, 1, 6);

    Label wn = new Label("Waiter's Name:");
    pane.add(wn, 0, 7);
    TextField twn = new TextField();
    pane.add(twn, 1, 7);

    // exception handling HERE!!!!!!!!!!!!!!!
    // ---------------------------------------------------------------------------------------------------------
    Label mi = new Label("Menu Items");
    mi.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    pane.add(mi, 0, 10);

    Label b = new Label("Beverage:");
    pane.add(b, 0, 11);
    pane.add(beverageComboBox, 1, 11);

    Label a = new Label("Appetizer:");
    pane.add(a, 0, 12);
    pane.add(appetizerComboBox, 1, 12);

    Label mc = new Label("Main Course:");
    pane.add(mc, 0, 13);
    pane.add(maincourseComboBox, 1, 13);

    Label d = new Label("Dessert:");
    pane.add(d, 0, 14);
    pane.add(dessertComboBox, 1, 14);
    // ----------------------------------------------------------------------------------------------------------
    Label bi = new Label("Bill");
    bi.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    pane.add(bi, 0, 17);

    Label st = new Label("Subtotal:");
    pane.add(st, 0, 18);
    TextField tst = new TextField();
    pane.add(tst, 1, 18);

    Label t = new Label("Tax:");
    pane.add(t, 0, 19);
    TextField tt = new TextField();
    pane.add(tt, 1, 19);

    Label to = new Label("Total:");
    pane.add(to, 0, 20);
    TextField tto = new TextField();
    pane.add(tto, 1, 20);
    // -----------------------------------------------------------------------------------------------------------
    Label w = new Label("Waiter:");
    pane.add(w, 0, 23);
    TextField tw = new TextField();
    pane.add(tw, 1, 23);

    Label tn2 = new Label("Table Num:");
    pane.add(tn2, 0, 24);
    TextField ttn = new TextField();
    pane.add(ttn, 1, 24);
    // ------------------------------------------------------------------------------------------------------------
    Button btn = new Button("    Clear Bill    ");
    pane.add(btn, 1, 30);
    // -------------------------------------------------------------------------------------------------------------

    Label io = new Label("Items Ordered");
    io.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    pane.add(io, 2, 3);
    // -----------------------------------------------------------------------------------------------------------------------
    Label bo = new Label("Beverage Ordered");
    pane.add(bo, 2, 5);

    ListView<String> list = new ListView<>();

    ObservableList<String> items1 = FXCollections.observableArrayList(beverages);
    // list.setItems(items1);
    beverageComboBox.getItems().addAll(items1);

    pane.add(list, 3, 5);
    GridPane.setRowSpan(list, 5);

    beverageComboBox.setOnAction(e -> setDisplay(items.indexOf(beverageComboBox.getValue())));

    /*
     * list.getSelectionModel().selectedItemProperty().addListener( ov -> {
     * pane.getChildren().clear(); for (Integer i:
     * list.getSelectionModel().getSelectedIndices()){
     * pane.getChildren().add(beverages[i]); } });
     */
    // ---------------------------------------------------------------------------------------------------------------
    Label ao = new Label("Appetizer Ordered");
    pane.add(ao, 2, 10);

    ListView<String> list2 = new ListView<>();

    ObservableList<String> items2 = FXCollections.observableArrayList();
    list2.setItems(items2);

    pane.add(list2, 3, 10);
    GridPane.setRowSpan(list2, 5);
    // ----------------------------------------------------------------------------------------------------------------
    Label mco = new Label("Main Course Ordered");
    pane.add(mco, 2, 15);

    ListView<String> list3 = new ListView<>();

    ObservableList<String> items3 = FXCollections.observableArrayList();
    list3.setItems(items3);

    pane.add(list3, 3, 15);
    GridPane.setRowSpan(list3, 5);
    // -------------------------------------------------------------------------------------------------------------------
    Label od = new Label("Dessert Ordered");
    pane.add(od, 2, 20);

    ListView<String> list4 = new ListView<>();

    ObservableList<String> items4 = FXCollections.observableArrayList();
    list4.setItems(items4);

    pane.add(list4, 3, 20);
    GridPane.setRowSpan(list4, 5);

    // Create a new scene and place it in the stage
    Scene scene = new Scene(pane, 800, 800);
    primaryStage.setTitle("Restaurant Bill Calculator");
    primaryStage.setScene(scene);
    primaryStage.show();

 }  
 }

1 个答案:

答案 0 :(得分:0)

beverageComboBox.setOnAction((e) -> {
    list.getItems().add(beverageComboBox.getSelectionModel().getSelectedItem());
});