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.
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 Blog */ 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("/test/control.css"); primaryStage.setVisible(true); } }
CSS(control.css)
.autofill-text {
-fx-skin: "np.com.ngopal.control.AutoFillTextBoxSkin";
}
Output
2. Filter Mode
You can easily set the Filter Mode on/off by using setFilterMode(boolean filter) . Let’s see the example of this FilterMode.
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 Blog */ 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("/test/control.css"); primaryStage.setVisible(true); } }
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.



#1 by Anatol on July 4, 2011 - 7:21 am
Quote
Hello! Tnx for useful post!
What’s a pity that I caught some errors on my configuration ( build32 x64 )
WARNING: com.sun.javafx.css.StyleManager$2 run Resource “null” not found.
SEVERE: javafx.scene.control.Control impl_processCSS The -fx-skin property has not been defined in CSS for AutoFillTextBox@78d59861[styleClass=autofill-text]
WARNING: com.sun.javafx.css.StyleManager$2 run Resource “null” not found.
SEVERE: javafx.scene.control.Control impl_processCSS The -fx-skin property has not been defined in CSS for AutoFillTextBox@78d59861[styleClass=autofill-text]
SEVERE: javafx.scene.control.Control impl_processCSS The -fx-skin property has not been defined in CSS for AutoFillTextBox@78d59861[styleClass=autofill-text]
WARNING: com.sun.javafx.css.StyleManager$2 run Resource “null” not found.
SEVERE: javafx.scene.control.Control impl_processCSS The -fx-skin property has not been defined in CSS for AutoFillTextBox@78d59861[styleClass=autofill-text]
If you ever met such behaviour, pleeease, would you make it clear for me?
#2 by Narayan Gopal Maharjan on July 4, 2011 - 6:25 pm
Quote
Hello Anatol,
Those WARNINGS are occured because you are using old Netbeans JavaFX 2.0 plugins .
Some of my recommendation:
>Please use new one.
>Or you can also start new Java Project not JavaFX project and add two jars(AutoFillTextBox.jar and jfxrt.jar) as library.
>Ensure that you have created and linked a css with contents as shown on above control.css
Thanks.
#3 by Anatol on July 5, 2011 - 11:28 pm
Quote
Thanks!
It works via Java-project ( not without some roughnesses, but works )
May I suggest little improvement from my side of view?
I used
//
hbox.setAlignment(Pos.CENTER);
//
and removed manual “translateXProperty()” for label
So, both nodes well aligned by HBox itself.
P.S.
When one items in list selected by mouse, list become empty. Is it by design?
Pingback: Java desktop links of the week, July 11 | Jonathan Giles
#4 by Michael on July 13, 2011 - 2:33 pm
Quote
Nice idea but on Windows 7, JDK 1.6u26, JavaFX 2.0 b34
I get a NPE when I type in a second letter in your first example.
So typing an “a” shows the output as shown here but when I then type “p” i get a NPE. I’d expected an updated list with all word starting with “ap”.
Michael
#5 by Narayan Gopal Maharjan on July 13, 2011 - 6:31 pm
Quote
Hi Michael,
You can send me details on privatejava@yahoo.com
#6 by Narayan Gopal Maharjan on July 14, 2011 - 11:31 pm
Quote
Hi Michael,
I’ve uploaded new jar please try with that jar.
Thanks.
Pingback: Source Code of AutoFill TextBox Released « Java and FX
#7 by Ricardo on September 23, 2011 - 8:10 pm
Quote
Thanks a lot!
I extended the AutoFillTextBoxSkin and did a litle change.
I need search for any part of the strings instead of just on the start.
I like it so much!
Thank you!
#8 by jimmy on October 13, 2011 - 4:45 pm
Quote
could you help me please…. provide the tutorial using your code. I cant understand what is Java Fx and how to use it…?
please… i need your help. Thanks
#9 by Narayan Gopal Maharjan on October 14, 2011 - 6:34 pm
Quote
You can know more about javafx at here: javafx.com
To get started with javafx programming goto : http://download.oracle.com/javafx/2.0/get_started/jfxpub-get_started.htm
#10 by Svuksic on November 4, 2011 - 7:54 pm
Quote
Hello it seems like a good and useful control but i got error: java.lang.ClassNotFoundException: javafx.scene.control.TextBox
Are you sure that in JFX 2.0 exist TextBox control or I missed something?
Could you help me?
#11 by Narayan Gopal Maharjan on November 4, 2011 - 8:20 pm
Quote
Hello this post was posted during beta release of javafx. So the TextBox is deprecated in current release. I’ll update this post in this week. But if you really want to implement this control then this post is very helpful. Because the source code of here are compatible with new one. http://blog.ngopal.com.np/2011/08/13/source-code-of-autofill-textbox-released/
#12 by Svuksic on November 4, 2011 - 10:44 pm
Quote
I saw that you replace textbox with textfield. I have built yours source project and added jar file as librari to my project and now have new error:
SEVERE: javafx.scene.control.Control impl_processCSS The -fx-skin property has not been defined in CSS for AutoFillTextBox@8608b9[styleClass=autofill-text]
Do you know where is problem? Sorry for lot of questions but I just stuck on this control but I really like it and I want use it in my project.
#13 by Narayan Gopal Maharjan on November 5, 2011 - 4:27 pm
Quote
I think you have not set the CSS class for the control. as shown in blog
#14 by Svuksic on November 5, 2011 - 6:39 pm
Quote
I don’t know where is problem, I’ll wait for yours release of control.
When I builded yours project I got warning: AutoFillTextBoxSkin.java uses or overrides a deprecated API maybe is problem in this.
#15 by Narayan Gopal Maharjan on November 5, 2011 - 10:17 pm
Quote
I think you are using some other approach. Please make a new project on Netbeans using javafxrt.jar as library and run the code. This will definetly work because it is working for me. You can give me your computer spec for solving your problem.
#16 by Svuksic on November 8, 2011 - 4:13 am
Quote
I opened yours project in Netbeans, to libraries add jfxrt library and started clean and build command. I got error :
warning: [options] bootstrap class path not set in conjunction with -source 1.6
Note: C:\AutoFillTextBox\src\np\com\ngopal\control\AutoFillTextBoxSkin.java uses or overrides a deprecated API.
After that add AutoFillTextBox.jar as library to my project.
Did you mean this as computer specification?
installed: JavaFX 2.0.1 SDK and Netbeans 7.1 beta.
#17 by Ajesh Baby on May 2, 2012 - 2:12 pm
Quote
Great work. Is it support other Locale and Unicode.
#18 by Narayan G. Maharjan on May 4, 2012 - 11:04 pm
Quote
I’ve not tried with the Unicodes. But I think it should work.
Pingback: JavaFX Drag and Drop Cell in ListView « Java and FX
#19 by Amol on May 17, 2012 - 2:35 pm
Quote
Dear Narayan ,
First of all thanks for posting your efforts
However, please let me know how to use/intigrate the custom component created in java into FXML.?
Thanks and Regards,
Amol
#20 by Narayan G. Maharjan on May 17, 2012 - 6:25 pm
Quote
Hi Amol,
I didn’t understand the “custom component in java”. Do you mean the swing/awt custom component? or the custom component of the javafx
Firstly if you want to include the control of the javafx in fxml then you just need to import the control in your fxml file.
Suppose: I’ve the autofill Textbox(np.com.ngopal.control.AutoFillTextBox) control to be added in the fxml then you just only need to import them in fxml like this.
<?import np.com.ngopal.autocomplete.* ?>
<AutoFillTextBox fx:id="filterPerson" prefWidth="200"/>
But if you are talking of adding the swing custom control then i think it’s probably little bit harder Hint: JFXPanel
Thanks
Narayan