1

I am using ControlFx Textfields binding which works find. The thing is that the Suggestion popup values that Contains the inputed value. Like if i type in "M" values will be "Emma", "max", "rosemary", "Mathew". So my main Question is how to make the Suggestion to only bring out values Starting with "M" like "max",""Mathew" This is my code. It select name from database and add them to an ArrayList

PreparedStatement ps=db.DataBase.getCon().prepareStatement("select name from STUDENTINFO");
ResultSet res=ps.executeQuery();

        List list=new ArrayList();
        while(res.next()){
            list.add(res.getString("name"));
        }
        TextFields.bindAutoCompletion(textfieldSearch,list);
fabian
  • 80,457
  • 12
  • 86
  • 114
kelvin andre
  • 395
  • 5
  • 11

1 Answers1

3

Here is a sample app that demos your question.

This snippet searches the original list for a substring and returns every item in the list that starts with the substring. this snippet should work with your code.

Key code:

TextFields.bindAutoCompletion(textFieldSearch, t -> {
    return list.stream().filter(elem -> 
    {
        return elem.toLowerCase().startsWith(t.getUserText().toLowerCase());
    }).collect(Collectors.toList());
});

Full app:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.controlsfx.control.textfield.TextFields;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication193 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        List<String> list = new ArrayList();
        list.add("Max");
        list.add("moon");
        list.add("am");
        list.add("two");

        TextField textFieldSearch = new TextField();
        TextFields.bindAutoCompletion(textFieldSearch, t -> {
            return list.stream().filter(elem
                    -> {
                return elem.toLowerCase().startsWith(t.getUserText().toLowerCase());
            }).collect(Collectors.toList());
        });

        StackPane root = new StackPane(textFieldSearch);
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}
SedJ601
  • 12,173
  • 3
  • 41
  • 59
  • thank you for your respond. but am get an error which says incompatible types: cannot infer type-variable(s) T (argument mismatch; bad return type in lambda expression Object cannot be converted to Collection) where T is a type-variable: T extends Object declared in method bindAutoCompletion(TextField,Callback>) The type of bindAutoCompletion(TextField,Callback>) is erroneous Am using netbeans IDE – kelvin andre May 25 '18 at 17:37
  • Add the `String` type to your List. It should look like mine – SedJ601 May 25 '18 at 17:56
  • 1
    Oh. The diamond reference, I have seen my error. Thanks – kelvin andre May 25 '18 at 22:10
  • 1
    Strange enough: I had the same error. I had to import `java.util.stream.Collectors`. – alexander Aug 26 '18 at 17:16