我尝试构建文本对话框,用户可以在其中输入事件名称,事件大小和选定的场地。
我的问题是如何收集输入;这是我到目前为止所做的:
eventName = new TextField();
eventSize = new TextField();
ObservableList<Venue> options =
FXCollections.observableArrayList(model.getVenuesList());
VeunueList = new ComboBox<Venue>(options);
我创建了一个封装所有输入的类:
public class MyResult {
String eventname;
String eventsize;
Venue venue;
}
我将变量定义为类Myresult
的对象:
private Dialog<MyResult> dialog ;
private Optional<MyResult> EventInput;
问题是如何在结果转换器中写入return语句;它给了我错误:
dialog.setResultConverter(dialogButton -> {
if (dialogButton == submit) {
return new MyResult(eventName.getText(),eventSize.getText(),VeunueList.getValue())
}
return null;
});
EventInput = dialog.showAndWait();
答案 0 :(得分:2)
目前尚不清楚你的片段出错的地方,但是对setResultConverter()
的调用来说正确的类型有时是有问题的。以下示例说明了Dialog
收集来自TextField
,DatePicker
和ComboBox<Venue>
的输入。在ComboBox<Venue>
中,Venue
的选择来自enum
,相应的ComboBox
模型是使用枚举的隐式values()
方法构建的。 resultConverter
属性Callback
返回Results
的新实例,其中包含各种视图组件的当前值。 Optional<Results>
显示了这些值ifPresent()
。可以在here和教程JavaFX improvements in Java SE 8u40.
控制台: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);
}
}