在JavaFX对话框中获取两个以上的输入 [英] Get more than two inputs in a JavaFX Dialog
问题描述
我尝试构建文本对话框,用户输入事件名称,事件大小和所选场地。
我的问题在于如何收集输入数据;这里到目前为止我做了什么:
eventName = new TextField();
eventSize = new TextField();
ObservableList< Venue> options =
FXCollections.observableArrayList(model.getVenuesList());
VeunueList = new ComboBox< Venue>(选项);
我创建了一个封装所有输入的类:
public class MyResult {
String eventname;
String eventsize;
场地;
}
我将变量定义为类的对象 Myresult
:
private Dialog< MyResult>对话;
private可选< MyResult> EventInput;
问题是如何在结果转换器中写入return语句;它给了我错误:
dialog.setResultConverter(dialogButton - > {
if(dialogButton == submit){
返回新的MyResult(eventName.getText(),eventSize.getText(),VeunueList.getValue())
}
返回null;
});
EventInput = dialog.showAndWait();
目前还不清楚你的片段出错的地方,但是这些类型更正为
控制台: 姓名2017-05-24其他地方
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 /
* /
公共类DialogTest扩展Application {
@Override
public void start(Stage primaryStage){
Dialog< Results> dialog = new Dialog<>();
dialog.setTitle(对话测试);
dialog.setHeaderText(请指定......);
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按钮) - > {
if(button == ButtonType.OK){
返回新结果(textField.getText(),
datePicker。 getValue(),comboBox.getValue());
}
返回null;
});
可选<结果> 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日期;
场地;
公共结果(字符串名称,LocalDate日期,场地地点){
this.text = name;
this.date = date;
this.venue =场地;
}
}
public static void main(String [] args){
launch(args);
}
}
I trying to build the text dialog where the user enter the event name, event size, and the selected the venue.
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();
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 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>
show those values ifPresent()
. Some related examples may be found here and in the tutorial, JavaFX improvements in Java SE 8u40.
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);
}
}
这篇关于在JavaFX对话框中获取两个以上的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!