在 JavaFX 对话框中获取两个以上的输入 [英] Get more than two inputs in a JavaFX Dialog
问题描述
我尝试构建文本对话框,用户可以在其中输入活动名称、活动规模和所选场地.
我的问题是如何收集输入;这是我到目前为止所做的:
eventName = new TextField();eventSize = new TextField();ObservableList<场地>选项 =FXCollections.observableArrayList(model.getVenuesList());VeunueList = new ComboBox(options);
我创建了一个封装我所有输入的类:
public class MyResult {字符串事件名称;字符串事件大小;场地场地;}
我将变量定义为 Myresult
类的对象:
私有对话框对话;私人 可选事件输入;
问题是如何在结果转换器中编写return语句;它给了我错误:
dialog.setResultConverter(dialogButton -> {如果(对话框按钮 == 提交){返回新的 MyResult(eventName.getText(),eventSize.getText(),VeunueList.getValue())}返回空;});EventInput = dialog.showAndWait();
目前尚不清楚您的片段哪里出错了,但可以为调用
控制台:Name 2017-05-24 Elsewhere
import java.time.LocalDate;导入 java.util.Optional;导入 javafx.application.Application;导入 javafx.application.Platform;导入 javafx.collections.FXCollections;导入 javafx.collections.ObservableList;导入 javafx.scene.control.ButtonType;导入 javafx.scene.control.ComboBox;导入 javafx.scene.control.DatePicker;导入 javafx.scene.control.Dialog;导入 javafx.scene.control.DialogPane;导入 javafx.scene.control.TextField;导入 javafx.scene.layout.VBox;导入 javafx.stage.Stage;/*** @see http://stackoverflow.com/q/44147595/230513* @see http://www.javaworld.com/article/2991463/*/公共类 DialogTest 扩展应用程序 {@覆盖公共无效开始(阶段primaryStage){对话框<结果>对话 = 新对话<>();dialog.setTitle("对话测试");dialog.setHeaderText("请说明...");DialogPane dialogPane = dialog.getDialogPane();dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);TextField textField = new TextField("名称");DatePicker datePicker = new DatePicker(LocalDate.now());ObservableList<场地>选项 =FXCollections.observableArrayList(Venue.values());ComboBox<场地>组合框 = 新组合框<>(选项);组合框.getSelectionModel().selectFirst();dialogPane.setContent(new VBox(8, textField, datePicker, comboBox));Platform.runLater(textField::requestFocus);dialog.setResultConverter((ButtonType button) -> {如果(按钮 == ButtonType.OK){返回新结果(textField.getText(),datePicker.getValue(),comboBox.getValue());}返回空;});可选的<结果>optionalResult = dialog.showAndWait();optionalResult.ifPresent((Results results) -> {System.out.println(results.text + " " + results.date + " " + results.venue);});}私人静态枚举地点 {Here, There, Elsewhere}私有静态类结果{字符串文本;本地日期;场地场地;公共结果(字符串名称,本地日期日期,场地地点){this.text = 名称;this.date = 日期;this.venue = 场地;}}公共静态无效主(字符串 [] 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 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.
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屋!