I am new to JAVA FX, I want to 'live' update one TextField based on another TextField value.
This is my snippet code:
#FXML
private void initialize() {
tf_code.textProperty().addListener((observable, oldValue, newValue) -> {
System.out.println(newValue.substring(2, 6));
tf_newCode.setText(newValue.substring(2, 6));
});
}
Should I add another listener to my second TextField ?
Works for me. Note that the below code does not require a .fxml file. Perhaps the call to method substring() in the code you posted is throwing an Exception that you are unaware of because you are catching it in an empty catch block? Of-course I'm only guessing since you only posted part of your code.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class JfxTst00 extends Application {
public void start(Stage mainStage) throws Exception {
mainStage.setTitle("JfxTst00");
BorderPane root = new BorderPane();
TextField tf_NewCode = new TextField();
TextField tf_Code = new TextField();
tf_Code.textProperty().addListener((observable, oldVal, newVal) -> tf_NewCode.setText(newVal));
root.setTop(tf_Code);
root.setBottom(tf_NewCode);
Scene scene = new Scene(root, 220, 70);
mainStage.setScene(scene);
mainStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Your questions does not actually explain the problem you're facing, though I see a few that you should be having.
First of all, you only need one listener for the first TextField because that is the one we are watching for changes.
Then you need to account for input into the TextField that is less than 2 characters and more than 6. Since you have set hard limits in your subString(2, 6) call, we only want our listener to work within those constraints.
Here is a simple text application that demonstrates:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TextFieldBinding extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
TextField txt1 = new TextField();
TextField txt2 = new TextField();
txt1.textProperty().addListener((observable, oldValue, newValue) -> {
// First, only update txt2 if the new value entered in txt1 is greater than 2, otherwise
// our substring() method will throw an exception
if (newValue.length() > 2) {
// We also need to prevent trying to get a substring that exceeds the remaining length
// of the txt1 input
int maxIndex;
if (newValue.length() < 6) {
maxIndex = newValue.length();
} else {
maxIndex = 6;
}
// Now set the text for txt2
txt2.setText(newValue.substring(2, maxIndex));
}
});
root.getChildren().addAll(txt1, txt2);
// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
Related
I am new to programming and am taking a class on OOP. I am trying to come up with a JavaFX application that saves mood input to be able to gather data. As of now the data is having to the console command window, but gets replaced when a new input is added. any Direction would be greatly appreciated.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class comboBoxPTSD extends Application {
Stage window;
Scene scene;
Button button;
ComboBox<String> comboBox;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("PTSD Tracker App");
button = new Button("Submit");
comboBox = new ComboBox<>();
comboBox.getItems().addAll(
"Happy", "Content", "Good", "Sad", "Angry",
"Hopeless", "Suicidal");
//sets combo boxes text name.
comboBox.setPromptText("How are you doing today?");
comboBox.setEditable(true);
/*
* can set the comboBox to input user input with
*"comboBox.setEditable(true);"
*will override box name.
*want to save input and output...
*
*use variable string.
*/
button.setOnAction(e -> printMood());
//ComboBoxes also generate actions if you need to get value instantly
comboBox.setOnAction( e -> System.out.println("User selected " + comboBox.getValue()) );
VBox layout = new VBox(10);
layout.setPadding(new Insets(20, 20, 20, 20));
layout.getChildren().addAll(comboBox, button);
scene = new Scene(layout, 300, 250);
window.setScene(scene);
window.show();
}
private void printMood(){
System.out.println(comboBox.getValue());
}
}
Application JavaFX
Consol picture
This code below which is triggered by a typed key into a JavaFX textfield is always behind one character. For instance, if user types in k, the string printed for searchBar.getText() equals "". If a user types in another k it will equal "k", and so on.
//this is triggered when a key is typed into search bar
#FXML public void onKeyPressedOnSearch(){
File[] fileCollection = model.getFileCollection();
System.out.println(searchBar.getText());
System.out.println(fileCollection[0].getName().substring(0, searchBar.getText().length()));
List<String> list = new ArrayList<String>();
ObservableList<String> tempObservableList = FXCollections.observableList(list);
/* for(int i = 0; i < fileCollection.length; i++){
if(!(searchBar.getText().equals(fileCollection[i].getName().substring(0, searchBar.getText().length())))){
tempObservableList.remove(i);
}
}
if(searchBar.getText() == null || searchBar.getText() == ""){
songList.setItems(observableList);
}else{
songList.setItems(tempObservableList);
} */
}
I would suggest to add a ChangeListener to the TextField, so you can grab any change in it from there, Please consider this example:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class CharByCharGrabbing extends Application {
#Override
public void start(Stage stage) throws Exception {
// create simple root and add two text fields to it
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
// just styling
root.setBackground(new Background(new BackgroundFill(Color.MAGENTA, null,null)));
TextField textField = new TextField();
TextField textField1 = new TextField();
root.getChildren().addAll(textField, textField1);
Scene scene = new Scene(root, 300,200);
stage.setScene(scene);
stage.setTitle("Grabbing Char by Char");
stage.show();
// Now you can add a change listener to the text property of the text field
// it will keep you updated with every single change (char by char)
// either adding to or removing from the TextField
textField.textProperty().addListener((observable, oldText, newText)->{ // lambda style
textField1.setText(newText); // update me when any change happens
// so you can grab the changes from here.
});
}
public static void main(String[] args) {
launch();
}
}
Test
I have this code sample that implement input mask to TextField:
package com.example;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) {
TextField field = new TextField() {
#Override public void replaceText(int start, int end, String text) {
super.replaceText(start, end, "#");
}
};
Button button = new Button("Show Text");
button.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event) {
System.out.println("TEXT: " + field.getText());
}
});
VBox root = new VBox(20);
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(field, button);
Scene scene = new Scene(root, 500, 500);
stage.setScene(scene);
stage.show();
}
}
Whenever I type the word "LIFE" in the TextField and press the button, the output always returns TEXT: ####, I want the output to return TEXT: LIFE. It seems like getText is not working. How to fix this?
Your approach doesn't work, because the replaceText method calls setText; so the implementation in your subclass causes the text field's textProperty to contain only '#' characters. Hence when you call getText(), you get a string with all '#' characters.
If you want to use a subclass of TextField like that, you would have to keep track of the "real" text elsewhere, which would get quite difficult. (Also, I think your implementation doesn't behave properly with copy and paste, and fixing that would be a bit tricky too.)
Probably the way to do this is to use a PasswordField, and replace it with a TextField when you want to show the text.
Here's a simple example:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class RevealPasswordExample extends Application {
#Override
public void start(Stage primaryStage) {
PasswordField passwordField = new PasswordField();
TextField textField = new TextField();
passwordField.textProperty().bindBidirectional(textField.textProperty());
StackPane textContainer = new StackPane(passwordField);
CheckBox showText = new CheckBox("Show text");
showText.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
if (isNowSelected) {
textContainer.getChildren().setAll(textField);
} else {
textContainer.getChildren().setAll(passwordField);
}
});
VBox root = new VBox(5, textContainer, showText);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(24));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Actually getText() is working properly, that's why you got #### at the output.
The thing is you have used replaceText() method, which will replace all the characters of your text field to '#' from the start to end. I donno why u have used that. If you don't wanna show the string typed in the field try it as a password field like,
PasswordField field = new PasswordField();
field.getText();
by using this also you can get the field text but it won't be visible to the user.
I have textfield that containing a value and that value should be Right alignment.
When i run the application it display the text left alignment but i set the Right alignment.
and problem with 3rd textfield.
After clicking the update button it works fine.
So may i know why it is behave different.
Code:
import java.io.File;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class TextFieldAlignment extends Application {
TextField rText;
File file;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
rText = new TextField("updated right1 updated right2 updated right3 updated right4");
rText.setAlignment(Pos.CENTER_RIGHT);
Button btn = new Button("update");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
rText.setText("updated right1 updated right2 updated right3 updated right4");
// applyWorkaround();
}
});
final Label labelFile = new Label();
Button btn2 = new Button();
btn2.setText("Open FileChooser'");
btn2.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("AVI files (*.exml)", "*.exml");
fileChooser.getExtensionFilters().add(extFilter);
//Show open file dialog
file = fileChooser.showOpenDialog(null);
// rText.setText(file.getPath());
}
});
VBox root = new VBox();
root.getChildren().addAll(rText, btn, btn2);
stage.setScene(new Scene(root, 200, 300));
stage.show();
}
}
Put the setalignment and settext after you add the TextField to the Scene.
Ref: Java API docs for Node
Node objects may be constructed and modified on any thread as long
they are not yet attached to a Scene. An application must attach nodes
to a Scene, and modify nodes that are already attached to a Scene, on
the JavaFX Application Thread.
If you create a binding between a JavaFX TextField and a property, then this binding is invalidated on every keystroke, which causes a change to the text.
If you have a chain of bindings the default behavior could cause problems, because in the middle of the editing values may be not valid.
Ok, I know I could create an uni-directional binding from the property to the textfield and register a change listener to get informed when the cursor leaves the field and update the property manually if necessary.
Is there an easy, elegant way to change this behavior so that the binding is only invalidated when the editing is complete, e.g. when the cursor leaves the field?
Thanks
I think you've pretty much described the only way to do it. Here's about the cleanest way I can see to implement it (using Java 8, though it's easy enough to convert the lambdas back to be JavaFX 2.2 compatible if you need):
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class CommitBoundTextField extends Application {
#Override
public void start(Stage primaryStage) {
TextField tf1 = new TextField();
createCommitBinding(tf1).addListener((obs, oldText, newText) ->
System.out.printf("Text 1 changed from \"%s\" to \"%s\"%n", oldText, newText));
TextField tf2 = new TextField();
createCommitBinding(tf2).addListener((obs, oldText, newText) ->
System.out.printf("Text 2 changed from \"%s\" to \"%s\"%n", oldText, newText));
VBox root = new VBox(5, tf1, tf2);
Scene scene = new Scene(root, 250, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
private StringBinding createCommitBinding(TextField textField) {
StringBinding binding = Bindings.createStringBinding(() -> textField.getText());
textField.addEventHandler(ActionEvent.ACTION, evt -> binding.invalidate());
textField.focusedProperty().addListener((obs, wasFocused, isFocused)-> {
if (! isFocused) binding.invalidate();
});
return binding ;
}
public static void main(String[] args) {
launch(args);
}
}
I realize that I am a little late with a response, but thought this might be useful to someone.
When using TextFields, I often attach a TextFormatter to help validate entries. You can attach a listener to the formatters' valueProperty. That property is updated when the text is committed, rather than on every keystroke.
Here's an example of what I am talking about using a TextField specialized for integer inputs. When you make edits in the text field, the changes will be reflected in the Label when you tap Enter, lose focus by clicking the button, switch to a different window, and so on.
import javafx.application.Application;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.converter.IntegerStringConverter;
class IntTextField extends TextField {
private final IntegerProperty value;
TextFormatter<Integer> formatter;
public double getValue() {
return value.getValue();
}
public void setValue(int newValue) {
value.setValue(newValue);
}
public IntegerProperty valueProperty() {
return value;
}
public StringBinding getStringBinding () {
return value.asString();
}
IntTextField(int initValue) {
value = new SimpleIntegerProperty(initValue);
setText(initValue + "");
formatter = new TextFormatter(new IntegerStringConverter(), initValue);
formatter.valueProperty().addListener((ObservableValue<? extends Integer> obs,
Integer oldValue, Integer newValue) -> value.setValue(newValue));
setTextFormatter(formatter);
}
IntTextField() {
this(0);
}
}
public class TFBindingDemo extends Application {
#Override
public void start(Stage stage) {
stage.setTitle("TFBindingDemo");
IntTextField intTextField = new IntTextField(12345);
intTextField.setMaxWidth(150);
Label label = new Label("Type in the TextField");
label.textProperty().bind(intTextField.getStringBinding());
Button removeFocusButton = new Button("Click Here to Remove Focus");
VBox root = new VBox(20, intTextField, label, removeFocusButton);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(20));
Scene scene = new Scene(root, 325, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}