Why JavaFX TextField listener repeats 3 times?











up vote
1
down vote

favorite












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







New contributor




Yerbol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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 at 14:16

















up vote
1
down vote

favorite












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







New contributor




Yerbol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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 at 14:16















up vote
1
down vote

favorite









up vote
1
down vote

favorite











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







New contributor




Yerbol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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







New contributor




Yerbol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Yerbol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Yerbol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 19 at 14:02









Yerbol

82




82




New contributor




Yerbol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Yerbol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Yerbol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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 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 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 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 at 14:16














1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










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 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',
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
});


}
});






Yerbol is a new contributor. Be nice, and check out our Code of Conduct.










 

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








up vote
3
down vote



accepted










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 at 16:42















up vote
3
down vote



accepted










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 at 16:42













up vote
3
down vote



accepted







up vote
3
down vote



accepted






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 at 14:39

























answered Nov 19 at 14:34









Zephyr

3,3172930




3,3172930












  • Thank you! It works!
    – Yerbol
    Nov 19 at 16:42


















  • Thank you! It works!
    – Yerbol
    Nov 19 at 16:42
















Thank you! It works!
– Yerbol
Nov 19 at 16:42




Thank you! It works!
– Yerbol
Nov 19 at 16:42










Yerbol is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Yerbol is a new contributor. Be nice, and check out our Code of Conduct.













Yerbol is a new contributor. Be nice, and check out our Code of Conduct.












Yerbol is a new contributor. Be nice, and check out our Code of Conduct.















 


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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'