Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

java - Get more than two inputs in a JavaFX Dialog

I trying to build the text dialog where the user enter the event name, event size, and the selected the venue.

enter image description here

My problem is in how can I gather the inputs; here what I did so far:

eventName = new TextField();
eventSize = new TextField();
ObservableList<Venue> options = 
            FXCollections.observableArrayList(model.getVenuesList());
VeunueList = new ComboBox<Venue>(options);

I create a class that encapsulate all my inputs:

public class MyResult {
    String eventname;
    String eventsize;
    Venue venue;
}

I define the variable to be object of class Myresult:

private Dialog<MyResult> dialog ;
private Optional<MyResult> EventInput;

The problem is how to write return statement in the result converter; it gives me error:

dialog.setResultConverter(dialogButton -> {
        if (dialogButton == submit) {
            return new MyResult(eventName.getText(),eventSize.getText(),VeunueList.getValue())
        }
        return null;
    });

    EventInput = dialog.showAndWait();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's not clear where your fragment goes awry, but getting the types correct for a call to setResultConverter() is sometimes problematical. The example below illustrates a Dialog that collects inputs from a TextField, DatePicker and ComboBox<Venue>. In the ComboBox<Venue>, the choice of Venue comes from an enum, and the corresponding ComboBox model is constructed using the enum's implicit values() method. The resultConverter property's Callback returns a new instance of Results having the current values of the various view components. The Optional<Results> shows those values ifPresent(). Some related examples may be found here and in the tutorial, JavaFX improvements in Java SE 8u40.

image

Console: Name 2017-05-24 Elsewhere

import java.time.LocalDate;
import java.util.Optional;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @see http://stackoverflow.com/q/44147595/230513
 * @see http://www.javaworld.com/article/2991463/
 */
public class DialogTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        Dialog<Results> dialog = new Dialog<>();
        dialog.setTitle("Dialog Test");
        dialog.setHeaderText("Please specify…");
        DialogPane dialogPane = dialog.getDialogPane();
        dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
        TextField textField = new TextField("Name");
        DatePicker datePicker = new DatePicker(LocalDate.now());
        ObservableList<Venue> options =
            FXCollections.observableArrayList(Venue.values());
        ComboBox<Venue> comboBox = new ComboBox<>(options);
        comboBox.getSelectionModel().selectFirst();
        dialogPane.setContent(new VBox(8, textField, datePicker, comboBox));
        Platform.runLater(textField::requestFocus);
        dialog.setResultConverter((ButtonType button) -> {
            if (button == ButtonType.OK) {
                return new Results(textField.getText(),
                    datePicker.getValue(), comboBox.getValue());
            }
            return null;
        });
        Optional<Results> optionalResult = dialog.showAndWait();
        optionalResult.ifPresent((Results results) -> {
            System.out.println(
                results.text + " " + results.date + " " + results.venue);
        });
    }

    private static enum Venue {Here, There, Elsewhere}

    private static class Results {

        String text;
        LocalDate date;
        Venue venue;

        public Results(String name, LocalDate date, Venue venue) {
            this.text = name;
            this.date = date;
            this.venue = venue;
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...