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.
javafx listener textfield keypress
New contributor
add a comment |
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.
javafx listener textfield keypress
New contributor
In your listener you modify the content of yourTextField
, then it triggers your listener :textField.setText(String.valueOf(textField.getText().charAt(0)));
. Everything works as coded.
– Pagbo
Nov 19 at 14:16
add a comment |
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.
javafx listener textfield keypress
New contributor
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
javafx listener textfield keypress
New contributor
New contributor
New contributor
asked Nov 19 at 14:02
Yerbol
82
82
New contributor
New contributor
In your listener you modify the content of yourTextField
, then it triggers your listener :textField.setText(String.valueOf(textField.getText().charAt(0)));
. Everything works as coded.
– Pagbo
Nov 19 at 14:16
add a comment |
In your listener you modify the content of yourTextField
, 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
add a comment |
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;
}));
Thank you! It works!
– Yerbol
Nov 19 at 16:42
add a comment |
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;
}));
Thank you! It works!
– Yerbol
Nov 19 at 16:42
add a comment |
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;
}));
Thank you! It works!
– Yerbol
Nov 19 at 16:42
add a comment |
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;
}));
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;
}));
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
add a comment |
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
add a comment |
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.
Yerbol is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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