Skip to content

Commit 81e1cc3

Browse files
Marius Hanlkevinrushforth
authored andcommitted
8286552: TextFormatter: UpdateValue/UpdateText is called, when no ValueConverter is set
Reviewed-by: kcr
1 parent 6c6545f commit 81e1cc3

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

modules/javafx.controls/src/main/java/javafx/scene/control/TextFormatter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -199,7 +199,7 @@ void unbindFromControl() {
199199
}
200200

201201
void updateValue(String text) {
202-
if (!value.isBound()) {
202+
if (valueConverter != null && !value.isBound()) {
203203
try {
204204
V v = valueConverter.fromString(text);
205205
setValue(v);

modules/javafx.controls/src/test/java/test/javafx/scene/control/TextFieldTest.java

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -61,6 +61,7 @@
6161
import javafx.scene.input.KeyEvent;
6262
import javafx.scene.layout.StackPane;
6363
import javafx.stage.Stage;
64+
import javafx.util.converter.IntegerStringConverter;
6465
import test.com.sun.javafx.pgstub.StubToolkit;
6566
import test.com.sun.javafx.scene.control.infrastructure.KeyEventFirer;
6667
import test.com.sun.javafx.scene.control.infrastructure.StageLoader;
@@ -474,6 +475,80 @@ public void testEnterWithConsumingActionHandler() {
474475
assertEquals(4, txtField.getSelection().getEnd());
475476
}
476477

478+
@Test
479+
public void testTextFormatterWithFilter() {
480+
txtField.setText("abc");
481+
txtField.setTextFormatter(new TextFormatter<>(this::upperCase));
482+
assertEquals("abc", txtField.getText());
483+
484+
// Set text again to trigger the text formatter filter.
485+
txtField.setText("abc");
486+
assertEquals("ABC", txtField.getText());
487+
}
488+
489+
@Test
490+
public void testTextFormatterWithConverter() {
491+
txtField.setText("200");
492+
txtField.setTextFormatter(new TextFormatter<>(new IntegerStringConverter() {
493+
@Override
494+
public Integer fromString(String value) {
495+
// Converter to integer and add 100.
496+
return super.fromString(value) + 100;
497+
}
498+
}));
499+
// No default value -> text is cleared.
500+
assertEquals("", txtField.getText());
501+
502+
txtField.setText("500");
503+
assertEquals("600", txtField.getText());
504+
}
505+
506+
@Test
507+
public void testTextFormatterWithConverterAndDefaultValue() {
508+
txtField.setText("200");
509+
txtField.setTextFormatter(new TextFormatter<>(new IntegerStringConverter() {
510+
@Override
511+
public Integer fromString(String value) {
512+
// Converter to integer and add 100.
513+
return super.fromString(value) + 100;
514+
}
515+
}, 1000));
516+
// Default value is set as text.
517+
assertEquals("1000", txtField.getText());
518+
519+
txtField.setText("500");
520+
assertEquals("600", txtField.getText());
521+
}
522+
523+
@Test
524+
public void testTextFormatterWithConverterAndFilter() {
525+
txtField.setText("200");
526+
txtField.setTextFormatter(new TextFormatter<>(new IntegerStringConverter() {
527+
@Override
528+
public Integer fromString(String value) {
529+
// Converter to integer and add 100.
530+
return super.fromString(value) + 100;
531+
}
532+
}, 1000, change -> {
533+
change.setText(change.getText().replace("3", ""));
534+
return change;
535+
}));
536+
// Default value is set as text.
537+
assertEquals("1000", txtField.getText());
538+
539+
txtField.setText("500");
540+
assertEquals("600", txtField.getText());
541+
542+
// 3 is removed, therefore we get 100. The value converter above will then add 100 (=200).
543+
txtField.setText("1300");
544+
assertEquals("200", txtField.getText());
545+
}
546+
547+
private Change upperCase(Change change) {
548+
change.setText(change.getText().toUpperCase());
549+
return change;
550+
}
551+
477552
private Change noDigits(Change change) {
478553
Change filtered = change.clone();
479554
filtered.setText(change.getText().replaceAll("[0-9]","\n"));

0 commit comments

Comments
 (0)