Updated on Dec 4, 2014: Published in Maven Repository javafx-autocomplete-field
Updated on Aug 5, 2014: Fixed code for JavaFX 8 [Github Repo]
Hello After working some couple of days on Javafx 2.0 Custom Control. I came to the final result of my Custom Control. The Custom Control which I’m going to tell you is about “Auto-Fill TextBox” . This control consists of two major controls they are TextBox and ListView with a Popup where Listview is embedded.
Features of this textbox
Let’s start these features one by one. To start using this control you can first download the Jar package or see Documentation first.
How to get Started
First download the jar file ‘AutoFillTextBox.jar’ from here. Then add the jar file to the Library of your current JavaFX Project where you want to implement this control. (PLEASE ADD jfxrt.jar ALSO TO YOUR PROJECT LIBRARY).
Now Let’s start the coding phase . On coding we’ll be working with the features presented in TextBox.
1. AutoFill Textbox on item selection with keys.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
package test; import np.com.ngopal.control.AutoFillTextBox; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.stage.Stage; /** * * @author Narayan G. Maharjan * @see <a href="https://blog.ngopal.com.np"> Blog </a> */ public class ControlTest extends Application { /** * @param args the command line arguments */ public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("AutoFillTextBox without FilterMode"); //SAMPLE DATA ObservableList data = FXCollections.observableArrayList(); String[] s = new String[]{"apple","ball","cat","doll","elephant", "fight","georgeous","height","ice","jug", "aplogize","bank","call","done","ego", "finger","giant","hollow","internet","jumbo", "kilo","lion","for","length","primary","stage", "scene","zoo","jumble","auto","text", "root","box","items","hip-hop","himalaya","nepal", "kathmandu","kirtipur","everest","buddha","epic","hotel"}; for(int j=0; j<s.length; j++){ data.add(s[j]); } //Layout HBox hbox = new HBox(); hbox.setSpacing(10); //CustomControl final AutoFillTextBox box = new AutoFillTextBox(data); //Label Label l = new Label("AutoFillTextBox: "); l.translateYProperty().set(5); l.translateXProperty().set(5); hbox.getChildren().addAll(l,box); Scene scene = new Scene(hbox,300,200); primaryStage.setScene(scene); scene.getStylesheets().add(getClass().getResource("control.css").toExternalForm()); primaryStage.show(); } } |
CSS(control.css)
1 2 3 |
.autofill-text { -fx-skin: "np.com.ngopal.control.AutoFillTextBoxSkin"; } |
Output
You can easily set the Filter Mode on/off by using setFilterMode(boolean filter) . Let’s see the example of this FilterMode.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package test; import np.com.ngopal.control.AutoFillTextBox; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.layout.VBox; import javafx.stage.Stage; /** * * @author Narayan G. Maharjan * @see <a href="https://blog.ngopal.com.np"> Blog </a> */ public class ControlTest extends Application { /** * @param args the command line arguments */ public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("AutoFillTextBox without FilterMode"); //SAMPLE DATA ObservableList<String> data = FXCollections.observableArrayList(); String[] s = new String[]{"apple","ball","cat","doll","elephant", "fight","georgeous","height","ice","jug", "aplogize","bank","call","done","ego", "finger","giant","hollow","internet","jumbo", "kilo","lion","for","length","primary","stage", "scene","zoo","jumble","auto","text", "root","box","items","hip-hop","himalaya","nepal", "kathmandu","kirtipur","everest","buddha","epic","hotel"}; for(int j=0; j<s.length; j++){ data.add(s[j]); } //Layout VBox vbox = new VBox(); vbox.setSpacing(10); //CustomControl final AutoFillTextBox box = new AutoFillTextBox(data); box.setListLimit(10); box.setFilterMode(true); box.getListview().setVisible(false); //Here we are using external Listview instead of AutoFillTextBox's ListView list = new ListView(); list.itemsProperty().bind(box.getListview().itemsProperty()); //Label Label l = new Label("AutoFillTextBox: (Filter Mode 'ON')"); l.translateYProperty().set(5); l.translateXProperty().set(5); vbox.getChildren().addAll(l,box,list); Scene scene = new Scene(vbox,300,200); primaryStage.setScene(scene); scene.getStylesheets().add(getClass().getResource("control.css").toExternalForm()); primaryStage.show(); } } |
CSS is just same as of previous example
Output:
Thanks for watching this blog. If you have any queries then please comment without hesitation.
Have a 🙂 good day.
For changelog you can see at top of the post
Hi ralfh,
I would recommend you to get access the source code available at here.
https://blog.ngopal.com.np/2011/08/13/source-code-of-autofill-textbox-released/ .
It contains the demo test application where you can see the real way of using this control.
Thanks
Narayan
Please refer to comment #46
But I dont know how to get value of text box when it is changed in MY CONTROLLER…plz cna you give me some code???
@Balkrushn,.getTextField() it is the textbox instance . You can add listener to the textProperty of that TextField. It is pretty easier.
Did you tried the
Thanks
Narayan
@Narayan,
Thanx dude…I’ll try it…
Thanks,
Balkrushn
Anytime 😉
Narayan thanks for the control.
My requirement was to fetch the data from server as and when user types the text in the control.
I customized the control to take a call back handler, which user can use to fetch data from server. Additionally, I added feature to show display-text in the drop-down, instead of the actual value of the text-field. Wish to contribute back to you. Suggest me how.
Hi Agilan Palani,
Wow that’s great. I guess you can contribute it from this github repo https://github.com/privatejava/javafx-autocomplete-field. Please mail me to [email protected]. I would be glad for your contribution.
Thanks
Narayan
Emailed.
Thanks 😉
Hi..i am displaying the list of objects in textbox and on final selection by mouse i want to display that object. Please suggest on what method i should call my autofill text box to display object
Hi Himani,.getListView().getSelectionModel().selectedItemProperty().addListener();
I guess you are going to make the selection event and display that selected object somewhere. To listen the event of selection list you can make the
Thanks
Hi Narayan,
where should i write this code excatly , when i want select a object from list ?
getListView().getSelectionModel().selectedItemProperty().addListener();
Hi beny,
Actually you can write those codes anywhere after the initialization of the autofill textbox object. The above code makes listening to the selection model of the ListView which is shown in the Autofill textbox while typing.
Thanks for your answer ,Can I ask some questions , should I send them Facebook,twitter or by Mail ?
Ni Narayan
thanks a lot for this great work.
I would like to know if there is a way to filter or search a type containts instead of a type begins with ?
Thanks for anh help
How can i find all of the words,which they have (for example) ‘a’ ?
For example:
aaa
abc
dsa
ola
….
Hi Narayan, I have tried to change your code, but it wasn’t successful:P , How can i find all of the words,which they have (for example) ‘a’ in class AutofillTextBoxSkin ?
For example:
aaa
abc
dsa
ola
….
Hi everyone,
Your code is great, but with JavaFX 8, it does not work.
Hi .
Thx for your work , is that a way to add this control in a fxml page ?
If you have to add control in fxml page then there is already a demo of FXML implementation inside the source code : https://github.com/privatejava/javafx-autocomplete-field/blob/master/src/test/FXML.fxml#L9
Well I have not tested yet! But I will push my updates on fx8 compatible soon : https://github.com/privatejava/javafx-autocomplete-field
Hi Narayan,
Thanks for this custom control. I have an issue where in items added to the List does appear for the first time. For e.g – I have a keyPressEvent on the control and on key press, I take the value of the text box and add five values to the list. But these values does not appear in the list for the FIRST time. When I press backspace and press the same key again this value appears. Can you please let me know a workaround for this or am I doing it in the wrong way? Here is the handler code.
@FXML AutoFillTextBox itemAutoFill;
@FXML public void hadleAutoFillKeyReleased(KeyEvent ke) {
ObservableList data = itemAutoFill.getData();
String text = itemAutoFill.getText();
if(StringUtils.isNotBlank(text)) {
for(int i=0; i<5; i++) {
data.add(text + i);
}
}
}
Hi Narayana,
Thanks for the post first.
I’m new to the JavaFX. I can’t imagine how to handle text change event.If you can provide code sample it will really help me.Thanks
Hi Sandun,
textProperty.addListener will help you very much or if you like to go for core then addEventFilter is useful.
Hi, not works for me in JavaFX 8. Error in AutoFillTextBoxBehavior and AutoFillTextBoxSkin files.
Type your comment here
I am sorry for late response. I have just cleaned the code and pushed all new codes for JavaFX 8. Please check the blog post for more details.
Thanks! I find too an interesting projet: http://fxexperience.com/controlsfx/features/ that includes Autocomplete, and other features as Dialog Boxs.
I will test.
Hi I have two questions:
1. with a database as: id_city, city_name…
How i can show city_name and recuperate id_city?
2. How i can use autocomplete with @FXML textfields?
Thanks.
How can apply focus loss event to autofill textbox?
Hi,
Your autocomplete sounds great, but can’t get it to work.There’s something wrong with loading the skin , Failed to load skin ‘np.com.ngopal.control.AutoFillTextBoxSkin’ with exception java.lang.ClassCastException..Do you have any hints about this?
I’m runing with java 8.20.
Try with [autoFillObj].getTextField().requestFocus();
You can open this github project there is a test package . It will run without exception 🙂
https://github.com/privatejava/javafx-autocomplete-field. I suspect you have not added the jar file in your classpath
Hey! Great work there! By the way, is there anyway to activate this feature for the first word only?
And is it possible to activate it based on the input of first word, eg: only activate when the first word entered is “search”
Thanks!
Hi,
I am using table view. is it possible to use the autofill in the editable columns of tableview ?
Yes you can do it , you can use them if you have customize cellrenderer
Hi,
I want to use this component in my software which is commercial software.
So is any license required to use this.
Please Suggest.
Thanks and regards,
Sasmita.
For this you can use the maven repository from http://maven-repository.com/artifact/np.com.ngopal/javafx-autocomplete-field/1.0.0
This has already been licensed with GNU .
Hi,
I am javafx 8 when I import your javafx project I get error in Skin Base import line in AutoFillTextBoxSkin, If change it to com.sun.javafx.scene.control.SkinBase; then the whole program is filled with error, Pls Help me With this…
Hi,
I am new in commercial license. So please clarify following points:
-How do I get the GNU license.
-Do I have to pay for it
-Can I extend the class and customize.
Please suggest,Your advice is valuable for me.
Thanks,
Sasmita
The component has major issues under jre8 update 40 – cannot delete editor text characters.
There is a github project for this : https://github.com/privatejava/javafx-autocomplete-field
Well until now jdk8u40 has not been released. I will look over the snapshot of that jdk and try that out.
Pingback: check
I’m using this jar in my application. But the ListView is displaying only 5 values even though I have more values in the textbox.
TextBox.getData() is returning more than 5 values correctly. But in the UI it shows only 5 values. Please help.
how to set text changed event to the AutoFillTextBox ???
1) First example. When I type first letter in TextBox, whole first word (starting from this letter) is inserting in TextBox, and cursor jumps to the end of word.
So to select any other word I can only use up/down arrows. Backspace doesn’t help (cause after erasing last letter autocompletion starts again and whole word restores)
Is it correct behaviour?
How to improve AutoFillTextBox so that it will be possible to type word letter, by letter ?
When FilterMode is ON only odd (???) appear in textbox.
When I type first letter all is OK.
When I type second letter it doesn’t appear in text box, but in log I see:
[ve]
added one,added one,
1=1::1
1=1::1
Changed:[ver, verify_repo_blocking]
When I type third letter it appears in text box, but second disappear, in log:
[vr]
Changed:[]
Filter mode is the process of filtering your list which can be displayble on any listview or other controls. But I am not sure this behaviour , can you checkout new source code available in github? -> https://github.com/privatejava/javafx-autocomplete-field