Sometime it required to set max length of a TextField, but unfortunately, fxml does not provide this functionality.
So how to do this ?
To set the max length, there are two ways.
1. Create a Change Listener class and register it , with TextField Object, for which you want to set max length.
2. Create a concrete class, and extend it with TextField class.
Here we are going to see how to implement first options that is -
1. Create a Change Listener class and register it , with TextField Object, for which you want to set max length.
Change Listener -
Implement ChangeListner and add it to the TextField, for which you want to set max character length.
To do that, create a concrete class, and implement it with javafx.beans.value.ChangeListener
This listener notice whenever state of text field (its observable) is changed (Here in this case a text field).
public class ChangeListener implements javafx.beans.value.ChangeListener<String>
Interface ChangeListener, have a method changed; this method is called whenever stated of observable text field is changed.
public void changed(ObservableValue<? extends String> observableValuem, String oldValue, String newValue)
In above method signature, parameter oldValue contains the old value of text field, and newValue parameter contains the changed value (new value).
We can add our code to control the max length of TextField like this -
public void changed(ObservableValue<? extends String> observableValue,
String oldValue, String newValue) {
/* To check if the value is null then return */
if (newValue == null){
return;
}
/* Check the length of new value length is less then defined max length then set the new value to the text field, else keep old value in text field. */
if (newValue.length() > maxLength) {
TextField.setText(oldValue);
} else {
TextField.setText(newValue);
}
} // End of Method
Register listener with text field -
In 'initialize' method of controller class, add listener like this -
textField.textProperty().addListener(new ChangeListener(TextField, maxLength));
In the above code snippet, textField is the text filed in which you want to add listener, and ‘maxLength’ is max length of text field.
Example -
fxml file -
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"
fx:controller="com.fxml.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="46.0" layoutY="51.0" minHeight="16"
minWidth="69" text="User Name" />
<TextField fx:id="userName" layoutX="142.0" layoutY="50.0"
prefHeight="25.0" prefWidth="139.0" promptText="Max Size is 5 Char" />
<Button layoutX="106.0" layoutY="88.0" mnemonicParsing="false" text="Ok" />
</children>
</AnchorPane>
Controller Class -
/*
* This is my controller class.
*/
package com.fxml;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import com.listener.ChangeListener;
/**
*
* @author vikas
*/
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private TextField userName;
@FXML
private void handleButtonAction(ActionEvent event) {
}
@Override
public void initialize(URL url, ResourceBundle rb) {
/* max length of text field user name */
int maxLength = 5;
/* add ChangeListner to TextField */
userName.textProperty().addListener(new ChangeListener(userName, maxLength));
}
} // End of class
Change Listener Class -
package com.listener;
/*
This listener calass is called to set maximum length of any text field.
Example -
TextField textField;
ChangeListener listener = new ChangeListener(TextField, 5); // set max length as 5
textField.textProperty().addListener(listener);
*/
import javafx.beans.value.ObservableValue;
import javafx.scene.control.TextField;
/**
*
* @author vikas
*/
public class ChangeListener implements javafx.beans.value.ChangeListener<String> {
private int maxLength;
private TextField textField;
public ChangeListener(TextField textField, int maxLength) {
this.textField= textField;
this.maxLength = maxLength;
}
public int getMaxLength() {
return maxLength;
}
@Override
public void changed(ObservableValue<? extends String> ov, String oldValue,
String newValue) {
if (newValue == null) {
return;
}
if (newValue.length() > maxLength) {
textField.setText(oldValue);
} else {
textField.setText(newValue);
}
}
}// End of Class
Main class -
/*
* Main class to load fxml class.
*/
package com.fxml;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author vikas
*/
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
} // End of Class
Output -
Input Maximum value in text field -
Thank You ...
can you send .war file of the project..??
ReplyDeletecan you send .war file of the project..??
ReplyDelete