我尝试构建文本对话框,用户可以在其中输入 Activity 名称、 Activity 规模和所选 field 。
我的问题是如何收集输入;这是我到目前为止所做的:
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();
最佳答案
尚不清楚您的片段在哪里出了问题,但为 setResultConverter()
的调用获取正确的类型有时是有问题的。下面的示例说明了 Dialog
从 TextField
收集输入, DatePicker
和ComboBox<Venue>
。在 ComboBox<Venue>
,选择Venue
来自enum
,以及相应的ComboBox
模型是使用枚举的隐式 values()
构建的方法。 resultConverter
属性(property)Callback
返回 Results
的新实例具有各个 View 组件的当前值。 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);
}
}
关于java - 在 JavaFX 对话框中获取两个以上的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44147595/