gpt4 book ai didi

JavaFX 日期选择器不更新值

转载 作者:搜寻专家 更新时间:2023-10-30 19:58:18 25 4
gpt4 key购买 nike

有点奇怪,我使用的是 JavaFX 8,我在 Jubula 测试期间遇到了一些奇怪的行为。

我有一个使用以下代码创建的日期选择器控件:

public DatePicker getDatePicker(DtDate defaultDate, int width){
DatePicker dtpckr = new DatePicker();
dtpckr.setMaxWidth(width);
dtpckr.setMinWidth(width);
dtpckr.setConverter(new StringConverter<LocalDate>() {
private DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy/MM/dd");
@Override
public String toString(LocalDate localDate) {
if(localDate==null)
return "";
return dateTimeFormatter.format(localDate);
}
@Override
public LocalDate fromString(String dateString) {
if(dateString==null || dateString.trim().isEmpty())
return null;
return LocalDate.parse(dateString,dateTimeFormatter);
}
});
dtpckr.setPromptText("yyyy/MM/dd");
dtpckr.setValue(LocalDate.parse(defaultDate.toString(), DateTimeFormatter.ofPattern("yyyy/MM/dd")));
return dtpckr;
}

工作正常,控件在窗体上创建并按预期工作。如果我使用日历选择日期,该值会更新。

现在,对于奇怪的部分,如果我在日期选择器中输入并手动输入日期(例如 2017/10/11),然后通过跳转或单击表单上的下一个文本框退出控件,日期选择器localdate 值不会更新。更新日期的唯一方法是按回车键强制日期选择器意识到存在新值。这会导致另一个问题,因为我有以下代码:

public void createSetDateAndTimeControls(){
DtDateAndTime currentDateAndTime;
try {
currentDateAndTime = systemstateController.getServerDateTime();
/*
* Creating controls to be used on the form
*/
int width = 150;
DatePicker dtpckr = getDatePicker(currentDateAndTime.date, width);
Label lblDate = new Label("Date:");
Label lblTimeHour = new Label("Hour:");
Label lblTimeMinute = new Label("Minute:");
Label lblTimeSecond = new Label("Second:");
TextField txtfldCurrentSetSecond = new TextField();
TextField txtfldCurrentSetMinute = new TextField();
TextField txtfldCurrentSetHour = new TextField();
Slider sldrHourPicker = getSlider(width, SliderType.Hour, txtfldCurrentSetHour, currentDateAndTime.time);
Slider sldrMinutePicker = getSlider(width, SliderType.Minute, txtfldCurrentSetMinute, currentDateAndTime.time);
Slider sldrSecondPicker = getSlider(width, SliderType.Second, txtfldCurrentSetSecond, currentDateAndTime.time);
/*
* Adding a grid pane to keep controls in correct place and aligned correctly
*/
GridPane grdpn = new GridPane();
grdpn.add(lblDate, 1, 1);
grdpn.add(dtpckr, 2, 1, 2,1);
grdpn.add(lblTimeHour, 1, 2);
grdpn.add(sldrHourPicker, 2, 2, 2 ,1);
grdpn.add(txtfldCurrentSetHour, 4, 2);
grdpn.add(lblTimeMinute, 1, 3);
grdpn.add(sldrMinutePicker, 2, 3, 2, 1);
grdpn.add(txtfldCurrentSetMinute, 4, 3);
grdpn.add(lblTimeSecond, 1, 4);
grdpn.add(sldrSecondPicker, 2, 4, 2, 1);
grdpn.add(txtfldCurrentSetSecond, 4, 4);
/*
* Creating buttons for user to press
*/
Button bttnOK = new Button("Change date and time");
bttnOK.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
if (checkIfAllDialogHasBeenFilledIn(grdpn)){
LocalDate test = dtpckr.getValue();
if (systemstateController.oeSetClock(ICrashUtils.setDateAndTime(dtpckr.getValue().getYear(), dtpckr.getValue().getMonthValue(), dtpckr.getValue().getDayOfMonth(),
(int)Math.floor(sldrHourPicker.getValue()), (int)Math.floor(sldrMinutePicker.getValue()), (int)Math.floor(sldrSecondPicker.getValue()))).getValue())
showOKMessage("Updated", "Date and time was updated successfully");
else
showErrorMessage("Error", "There was an error updating the date and time");
}
else
showUserCancelledActionPopup();
} catch (ServerOfflineException | ServerNotBoundException e) {
showExceptionErrorMessage(e);
}
}
});
bttnOK.setDefaultButton(true);
grdpn.add(bttnOK, 2, 5, 3, 1);
Button bttnSetNow = new Button("Now");
bttnSetNow.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
dtpckr.setValue(LocalDate.now());
LocalTime time = LocalTime.now();
sldrHourPicker.setValue(time.getHour());
sldrMinutePicker.setValue(time.getMinute());
sldrSecondPicker.setValue(time.getSecond());
}
});

grdpn.add(bttnSetNow, 4, 1);
/*
* End of creating controls to be used on the form
*/
anchrpnActivator.getChildren().add(grdpn);
AnchorPane.setTopAnchor(grdpn, 0.0);
AnchorPane.setRightAnchor(grdpn, 0.0);
AnchorPane.setLeftAnchor(grdpn, 0.0);
AnchorPane.setBottomAnchor(grdpn, 0.0);
} catch (ServerOfflineException | ServerNotBoundException e) {
showExceptionErrorMessage(e);
}
}

其中有以下行:

bttnOK.setDefaultButton(true);

这意味着如果用户在日期选择器控件上输入文本期间按下回车键,它会在用户将数据放入其他控件之前触发 onaction 事件,而我不会'特别想要那个。

所以问题是,如果用户在框中输入文本然后在未按回车键的情况下离开框,为什么 dtpckr.getValue() 不会更改?我找不到一个事件来调用用户离开日期选择器框或日期选择器的属性来获取控件的文本值并将其与 dtpckr.getValue() 的值进行比较。我在这里缺少什么吗?

如果您需要更多信息,请告诉我!

[编辑]

It appears this might be a bug in the current datepicker setup in JavaFX

我可能必须删除默认按钮并在 Jubula 测试中按下回车键才能使其正常工作。除非有人有替代方案?

最佳答案

The bug log that I added above had the answer .您可以通过以下代码访问文本框的字符串值:

datePicker.getEditor().getText();

因此可以通过以下方式设置文本框值:

datePicker.setValue(datePicker.getConverter()
.fromString(datePicker.getEditor().getText()));

我正在向失去焦点事件添加一个事件,这将强制更新日期选择器值


和工作代码:

public DatePicker getDatePicker(DtDate defaultDate, int width){
DatePicker dtpckr = new DatePicker();
dtpckr.setMaxWidth(width);
dtpckr.setMinWidth(width);
dtpckr.setConverter(new StringConverter<LocalDate>() {
private DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy/MM/dd");
@Override
public String toString(LocalDate localDate) {
if(localDate==null)
return "";
return dateTimeFormatter.format(localDate);
}
@Override
public LocalDate fromString(String dateString) {
if(dateString==null || dateString.trim().isEmpty())
return null;
try{
return LocalDate.parse(dateString,dateTimeFormatter);
}
catch(Exception e){
//Bad date value entered
return null;
}
}
});
dtpckr.setPromptText("yyyy/MM/dd");
dtpckr.setValue(LocalDate.parse(defaultDate.toString(), DateTimeFormatter.ofPattern("yyyy/MM/dd")));
//This deals with the bug located here where the datepicker value is not updated on focus lost
//https://bugs.openjdk.java.net/browse/JDK-8092295?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
dtpckr.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (!newValue){
dtpckr.setValue(dtpckr.getConverter().fromString(dtpckr.getEditor().getText()));
}
}
});
return dtpckr;
}

关于JavaFX 日期选择器不更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32346893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com