I have a SearchableComboBox from ControlsFX where I want to type in the name of an object, and a listener would put that text into an SQL request to fetch all objects fitting the text, and put those objects as options on the SearchableComboBox. Problem is, I can't find a way to get access to the text in the textfield.
myComboBox.getEditor().getText() works for a ComboBox, but not for a SearchableComboBox.
Any idea of how I could do that? Of course I could simply use an editable ComboBox, but that's not exactly what I'm looking for.
HelloController.java
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import org.controlsfx.control.SearchableComboBox;
public class HelloController {
@FXML
private SearchableComboBox searchableCombobox;
@FXML
private ComboBox comboBox;
@FXML
private void initialize(){
comboBox.setOnKeyReleased(obs -> System.out.println(comboBox.getEditor().getText()));
searchableCombobox.setOnKeyReleased(obs -> System.out.println(searchableCombobox.getEditor().getText()));
}
}
HelloApplication.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
hello-view.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import org.controlsfx.control.SearchableComboBox?>
<VBox alignment="CENTER" prefHeight="285.0" prefWidth="327.0" spacing="20.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/18" fx:controller="com.example.sampleapp.HelloController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
<ComboBox fx:id="comboBox" editable="true" prefWidth="150.0" />
<SearchableComboBox fx:id="searchableCombobox" />
<Label fx:id="welcomeText" />
</VBox>