java多人聊天室javafx,在JavaFX对话框中获取两个以上的输入

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

ZBwRS.png

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

eventName = new TextField();

eventSize = new TextField();

ObservableList options =

FXCollections.observableArrayList(model.getVenuesList());

VeunueList = new ComboBox(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 dialog ;

private Optional 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. In the ComboBox, 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 show those values ifPresent(). Some related examples may be found here and in the tutorial, JavaFX improvements in Java SE 8u40.

A8mrj.png

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 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 options =

FXCollections.observableArrayList(Venue.values());

ComboBox 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 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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值