import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.Stage; import javafx.scene.text.TextAlignment; import javafx.scene.text.Font; import javafx.geometry.Pos; import javafx.geometry.Insets; import javafx.scene.layout.Border; import javafx.scene.layout.BorderStroke; import javafx.scene.layout.BorderStrokeStyle; import javafx.scene.layout.BorderWidths; import javafx.scene.paint.Color; import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.event.Event; import javafx.beans.value.*; // ChangeListener public class UIBspChoiceBox01 extends Application implements EventHandler{ private ChoiceBox choicePizzaName = new ChoiceBox () ; private CheckBox chkMitZwiebel = new CheckBox("Mit Zwiebeln") ; private CheckBox chkMitMais = new CheckBox("Mit Mais") ; private TextField tErg = new TextField("") ; private Button bnOk = new Button("Ok") ; private Button bnEsc = new Button("Esc") ; @Override public void start(Stage stage) { // https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/VBox.html // https://docs.oracle.com/javase/8/javafx/api/toc.htm VBox root = new VBox(22); root.setAlignment(Pos.CENTER); root.setFillWidth(true); choicePizzaName.getItems().add("Käse"); choicePizzaName.getItems().add("Käse Schinken"); choicePizzaName.getItems().add("Hawai"); choicePizzaName.getItems().add("Salami / Schinken"); choicePizzaName.getItems().add("Vegetarisch"); //choicePizzaName.setFont(new Font("Courier New",22)); HBox hbox = createChoiceBox(choicePizzaName , "Name der Pizza"); root.getChildren().add(hbox); //import javafx.beans.value.*; // ChangeListener choicePizzaName.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observableValue, Number number, Number number2) { showChoicePizzaName(number2); } }); hbox = createButtonBase(chkMitZwiebel); root.getChildren().add(hbox); hbox = createButtonBase(chkMitMais ); root.getChildren().add(hbox); hbox = createTextInputControl(tErg, "Ergebnis"); root.getChildren().add(hbox); FlowPane boxpane = new FlowPane(20,20); boxpane.setAlignment(Pos.CENTER); boxpane.setMaxWidth(Double.POSITIVE_INFINITY); root.getChildren().add(boxpane); bnOk.setFont(new Font("Courier New",22)); bnOk.setMaxWidth(Double.POSITIVE_INFINITY); bnOk.setOnAction(this); boxpane.getChildren().add(bnOk); boxpane.setMargin(bnOk, new Insets(0, 10, 0, 10) ); // TRBL bnEsc.setFont(new Font("Courier New",22)); bnEsc.setMaxWidth(Double.POSITIVE_INFINITY); bnEsc.setOnAction(this); boxpane.getChildren().add(bnEsc); boxpane.setMargin(bnEsc, new Insets(0, 10, 0, 10) ); // TRBL Scene scene= new Scene(root, 660, 300); scene.getStylesheets().add("UIBspChoiceBox01.css"); stage.setTitle("UIBspChoiceBox01"); stage.setScene(scene); stage.show(); } private HBox createTextInputControl(TextInputControl control, String caption) { HBox hbox = new HBox(22); hbox.setFillHeight(true); hbox.setMaxWidth(Double.POSITIVE_INFINITY); Label label = new Label(caption) ; label.setFont(new Font("Courier New",22)); hbox.getChildren().add(label); hbox.setMargin(label, new Insets(5, 0, 0, 10) ); // TRBL control.setFont(new Font("Courier New",22)); control.setMaxWidth(Double.POSITIVE_INFINITY); hbox.getChildren().add(control); hbox.setHgrow(control, Priority.ALWAYS); hbox.setMargin(control, new Insets(0, 10, 0, 10) ); // TRBL return hbox; } private HBox createChoiceBox(ChoiceBox control, String caption) { HBox hbox = new HBox(22); hbox.setFillHeight(true); hbox.setMaxWidth(Double.POSITIVE_INFINITY); Label label = new Label(caption) ; label.setFont(new Font("Courier New",22)); hbox.getChildren().add(label); hbox.setMargin(label, new Insets(5, 0, 0, 10) ); // TRBL // control.setFont(new Font("Courier New",22)); // gibt es nicht ?? control.setMaxWidth(Double.POSITIVE_INFINITY); hbox.getChildren().add(control); hbox.setHgrow(control, Priority.ALWAYS); hbox.setMargin(control, new Insets(0, 10, 0, 10) ); // TRBL return hbox; } private HBox createButtonBase(ButtonBase control) { HBox hbox = new HBox(22); hbox.setFillHeight(true); hbox.setMaxWidth(Double.POSITIVE_INFINITY); control.setFont(new Font("Courier New",22)); control.setMaxWidth(Double.POSITIVE_INFINITY); hbox.getChildren().add(control); hbox.setHgrow(control, Priority.ALWAYS); hbox.setMargin(control, new Insets(0, 10, 0, 10) ); // TRBL return hbox; } private void showChoicePizzaName(Number index) { System.out.println("choicePizzaName: index: "+ index); } @Override public void handle(Event e) { if(e.getSource() == bnOk){ calc(); } if(e.getSource() == bnEsc){ Platform.exit(); } } private void calc() { StringBuilder sb = new StringBuilder(100); String pizza = (String)choicePizzaName.getValue(); if (pizza==null) { errorBox("Bitte geben Sie einen Pizzanamen ein","Hinweis"); return; } sb.append(pizza); sb.append(": "); if (chkMitZwiebel.isSelected()) { sb.append(", mit Zwiebeln "); } if (chkMitMais.isSelected()) { sb.append(", mit Mais "); } tErg.setText(sb.toString()); } public void errorBox( String message, String title) { Dialog alert = new Alert(Alert.AlertType.ERROR,message); alert.setTitle(title); alert.setHeaderText(title); alert.setResizable(true); alert.show(); // a.setContentText(content); } // ErrorBox public static void main(String[] argv) { launch(argv); } }