wuz
wuz

Reputation: 513

Providing the item collection to a custom extended combobox in java FXML

I am reaching out because I get a NochSuchMethodException when using my custom FilterComboBox because I don't provide an empty constructor. The constructor expects the Items of the extended combobox.

Combobox control:

import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;

public class FilterComboBoxTB extends ComboBox<String> {
	
	private ObservableList<String> initialList;
	
	public FilterComboBoxTB(ObservableList<String> items) {
        super(items);
        super.setEditable(true);
        this.initialList = items;
        this.configAutoFilterListener();
    }
	
	private void configAutoFilterListener() {
		final FilterComboBoxTB currentInstance = this;
        this.getEditor().textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
            	final TextField editor = currentInstance.getEditor();
                final String selected = currentInstance.getSelectionModel().getSelectedItem();
            	if (selected == null || !selected.equals(editor.getText())) {
            		filterItems(newValue, currentInstance);
                	currentInstance.show();
            	}
            	
            }
        });
    }
	
  private void filterItems(String filter, ComboBox<String> comboBox) {
	  ObservableList<String> filteredList = this.readFromList(filter, initialList);
      comboBox.setItems(filteredList);
    }
	
	private ObservableList<String> readFromList(String filter, ObservableList<String> originalList) {
        ObservableList<String> filteredList = FXCollections.observableArrayList();
        for (String item : originalList) {
        	if (item.toLowerCase().indexOf(filter.toLowerCase())>-1) {    
        		filteredList.add(item);
            }
        }
        return filteredList;
    }
}

FXML-Datei:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Label?>
<?import presentation.controls.FilterComboBoxTB?>

<BorderPane>
	<top>
        <FilterComboBoxTB **Method="GetItemsToPassToConstructor()"** />
    </top>
    <center>
        <Label text="Some data here"/>
    </center>
</BorderPane>

How do I specify the method in the FXML to get the relevant parameter? Thanks a lot and best regards

Upvotes: 2

Views: 723

Answers (1)

James_D
James_D

Reputation: 209724

In JavaFX 8 (but not in earlier versions) you can do this by annotating the constructor parameter with @NamedArg. If you update your control as follows:

public class FilterComboBoxTB extends ComboBox<String> {

    private ObservableList<String> initialList;

    public FilterComboBoxTB(@NamedArg("items") ObservableList<String> items) {
        super(items);
        // code as before ...
    }

    // ...
}

Then in your FXML do

<FilterComboBoxTB>
    <items>
        <FXCollections fx:factory="observableArrayList">
            <String value="Item 1" />
            <String value="Item 2" />
            <String value="Item 3" />
        </FXCollections>
    </items>
</FilterComboBoxTB>

(with all the necessary imports).

Upvotes: 1

Related Questions