Why JavaFX TextField listener repeats 3 times?












1















I am trying to track input to TextField and allow user to input only 1 symbol per TextField, here is my code:



package sample;

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
TextField textField = new TextField(); //creating new textfield
Pane window = new Pane();
Parent root = window;
window.getChildren().addAll(textField); //adding textfield to the window
primaryStage.setScene(new Scene(root, 200, 50));
primaryStage.show();
textField.textProperty().addListener(event ->
{
try {
if (textField.getLength() > 1) { //check if the length of the textfield text exceeds 1
System.out.println("NOT Accepted");
textField.setText(String.valueOf(textField.getText().charAt(0))); //set textfield text to first char only
} else {
System.out.println("Accepted");
}
} catch (IndexOutOfBoundsException Bound) {}
}
);
}

public static void main(String args) {
launch(args);
}
}


When I press any symbol first time, everything works good, but when I press second time, event listener repeats 3 times. Here the exapmle:
"a" key has been pressed, console output:



Accepted  //<---Correct


"a" key (or any another key) has been pressed second time, console output:



NOT Accepted  //<---Correct
Accepted //<---Not correct
Accepted //<---Not correct


As shown above listener repeats 3 times.
But I expect that console should show only "NOT Accepted" and shouldn't repeat 2 times more.










share|improve this question























  • In your listener you modify the content of your TextField, then it triggers your listener : textField.setText(String.valueOf(textField.getText().charAt(0)));. Everything works as coded.

    – Pagbo
    Nov 19 '18 at 14:16


















1















I am trying to track input to TextField and allow user to input only 1 symbol per TextField, here is my code:



package sample;

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
TextField textField = new TextField(); //creating new textfield
Pane window = new Pane();
Parent root = window;
window.getChildren().addAll(textField); //adding textfield to the window
primaryStage.setScene(new Scene(root, 200, 50));
primaryStage.show();
textField.textProperty().addListener(event ->
{
try {
if (textField.getLength() > 1) { //check if the length of the textfield text exceeds 1
System.out.println("NOT Accepted");
textField.setText(String.valueOf(textField.getText().charAt(0))); //set textfield text to first char only
} else {
System.out.println("Accepted");
}
} catch (IndexOutOfBoundsException Bound) {}
}
);
}

public static void main(String args) {
launch(args);
}
}


When I press any symbol first time, everything works good, but when I press second time, event listener repeats 3 times. Here the exapmle:
"a" key has been pressed, console output:



Accepted  //<---Correct


"a" key (or any another key) has been pressed second time, console output:



NOT Accepted  //<---Correct
Accepted //<---Not correct
Accepted //<---Not correct


As shown above listener repeats 3 times.
But I expect that console should show only "NOT Accepted" and shouldn't repeat 2 times more.










share|improve this question























  • In your listener you modify the content of your TextField, then it triggers your listener : textField.setText(String.valueOf(textField.getText().charAt(0)));. Everything works as coded.

    – Pagbo
    Nov 19 '18 at 14:16
















1












1








1








I am trying to track input to TextField and allow user to input only 1 symbol per TextField, here is my code:



package sample;

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
TextField textField = new TextField(); //creating new textfield
Pane window = new Pane();
Parent root = window;
window.getChildren().addAll(textField); //adding textfield to the window
primaryStage.setScene(new Scene(root, 200, 50));
primaryStage.show();
textField.textProperty().addListener(event ->
{
try {
if (textField.getLength() > 1) { //check if the length of the textfield text exceeds 1
System.out.println("NOT Accepted");
textField.setText(String.valueOf(textField.getText().charAt(0))); //set textfield text to first char only
} else {
System.out.println("Accepted");
}
} catch (IndexOutOfBoundsException Bound) {}
}
);
}

public static void main(String args) {
launch(args);
}
}


When I press any symbol first time, everything works good, but when I press second time, event listener repeats 3 times. Here the exapmle:
"a" key has been pressed, console output:



Accepted  //<---Correct


"a" key (or any another key) has been pressed second time, console output:



NOT Accepted  //<---Correct
Accepted //<---Not correct
Accepted //<---Not correct


As shown above listener repeats 3 times.
But I expect that console should show only "NOT Accepted" and shouldn't repeat 2 times more.










share|improve this question














I am trying to track input to TextField and allow user to input only 1 symbol per TextField, here is my code:



package sample;

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
TextField textField = new TextField(); //creating new textfield
Pane window = new Pane();
Parent root = window;
window.getChildren().addAll(textField); //adding textfield to the window
primaryStage.setScene(new Scene(root, 200, 50));
primaryStage.show();
textField.textProperty().addListener(event ->
{
try {
if (textField.getLength() > 1) { //check if the length of the textfield text exceeds 1
System.out.println("NOT Accepted");
textField.setText(String.valueOf(textField.getText().charAt(0))); //set textfield text to first char only
} else {
System.out.println("Accepted");
}
} catch (IndexOutOfBoundsException Bound) {}
}
);
}

public static void main(String args) {
launch(args);
}
}


When I press any symbol first time, everything works good, but when I press second time, event listener repeats 3 times. Here the exapmle:
"a" key has been pressed, console output:



Accepted  //<---Correct


"a" key (or any another key) has been pressed second time, console output:



NOT Accepted  //<---Correct
Accepted //<---Not correct
Accepted //<---Not correct


As shown above listener repeats 3 times.
But I expect that console should show only "NOT Accepted" and shouldn't repeat 2 times more.







javafx listener textfield keypress






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 14:02









YerbolYerbol

82




82













  • In your listener you modify the content of your TextField, then it triggers your listener : textField.setText(String.valueOf(textField.getText().charAt(0)));. Everything works as coded.

    – Pagbo
    Nov 19 '18 at 14:16





















  • In your listener you modify the content of your TextField, then it triggers your listener : textField.setText(String.valueOf(textField.getText().charAt(0)));. Everything works as coded.

    – Pagbo
    Nov 19 '18 at 14:16



















In your listener you modify the content of your TextField, then it triggers your listener : textField.setText(String.valueOf(textField.getText().charAt(0)));. Everything works as coded.

– Pagbo
Nov 19 '18 at 14:16







In your listener you modify the content of your TextField, then it triggers your listener : textField.setText(String.valueOf(textField.getText().charAt(0)));. Everything works as coded.

– Pagbo
Nov 19 '18 at 14:16














1 Answer
1






active

oldest

votes


















3














Instead of using a ChangeListener, I recommend a simple TextFormatter instead. This allows you to prevent the change without triggering another event.



    textField.setTextFormatter(new TextFormatter<String>((TextFormatter.Change change) -> {
String newText = change.getControlNewText();
if (newText.length() == 1) {
System.out.println("Accepted");
} else if (newText.length() > 1) {
System.out.println("NOT Accepted");
return null;
}

return change;
}));





share|improve this answer


























  • Thank you! It works!

    – Yerbol
    Nov 19 '18 at 16:42











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376317%2fwhy-javafx-textfield-listener-repeats-3-times%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Instead of using a ChangeListener, I recommend a simple TextFormatter instead. This allows you to prevent the change without triggering another event.



    textField.setTextFormatter(new TextFormatter<String>((TextFormatter.Change change) -> {
String newText = change.getControlNewText();
if (newText.length() == 1) {
System.out.println("Accepted");
} else if (newText.length() > 1) {
System.out.println("NOT Accepted");
return null;
}

return change;
}));





share|improve this answer


























  • Thank you! It works!

    – Yerbol
    Nov 19 '18 at 16:42
















3














Instead of using a ChangeListener, I recommend a simple TextFormatter instead. This allows you to prevent the change without triggering another event.



    textField.setTextFormatter(new TextFormatter<String>((TextFormatter.Change change) -> {
String newText = change.getControlNewText();
if (newText.length() == 1) {
System.out.println("Accepted");
} else if (newText.length() > 1) {
System.out.println("NOT Accepted");
return null;
}

return change;
}));





share|improve this answer


























  • Thank you! It works!

    – Yerbol
    Nov 19 '18 at 16:42














3












3








3







Instead of using a ChangeListener, I recommend a simple TextFormatter instead. This allows you to prevent the change without triggering another event.



    textField.setTextFormatter(new TextFormatter<String>((TextFormatter.Change change) -> {
String newText = change.getControlNewText();
if (newText.length() == 1) {
System.out.println("Accepted");
} else if (newText.length() > 1) {
System.out.println("NOT Accepted");
return null;
}

return change;
}));





share|improve this answer















Instead of using a ChangeListener, I recommend a simple TextFormatter instead. This allows you to prevent the change without triggering another event.



    textField.setTextFormatter(new TextFormatter<String>((TextFormatter.Change change) -> {
String newText = change.getControlNewText();
if (newText.length() == 1) {
System.out.println("Accepted");
} else if (newText.length() > 1) {
System.out.println("NOT Accepted");
return null;
}

return change;
}));






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 14:39

























answered Nov 19 '18 at 14:34









ZephyrZephyr

3,8382931




3,8382931













  • Thank you! It works!

    – Yerbol
    Nov 19 '18 at 16:42



















  • Thank you! It works!

    – Yerbol
    Nov 19 '18 at 16:42

















Thank you! It works!

– Yerbol
Nov 19 '18 at 16:42





Thank you! It works!

– Yerbol
Nov 19 '18 at 16:42




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376317%2fwhy-javafx-textfield-listener-repeats-3-times%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()