JavaFx Autocompletion Listener radiobutton

时间:2018-06-12 17:35:10

标签: java javafx java-8 javafx-8 listener

我有一个文本字段,我希望以动态方式更改自动完成列表。 所有代码都在我的控制器的初始化方法中。

这是我第一次运行程序时的原始分配:( nomi是文本字段,listaNomi是ObservableList

listaNomi = OperatoreDAO.cercaNomiOp(attuale);
                TextFields.bindAutoCompletion(nomi,listaNomi);

这有效,但现在我想在选择两个不同的RadioButton中的一个时更改列表内容(OperatoreDAO.cercaNomiOp方法将RadioButton作为参数)

                contrattoGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {

                     @Override
                public void changed(ObservableValue<? extends Toggle> ov, Toggle t, Toggle t1) {
                    RadioButton selez = (RadioButton)t1.getToggleGroup().getSelectedToggle();

                    try {
                        listaNomi = OperatoreDAO.cercaNomiOp(selez);
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            });

我必须将ObservableList<String> listaNomi = FXCollections.observableArrayList();放在initialize方法之外,因为本地变量必须是最终的。 现在它没有说错误,但是如果我改变radiobutton,列表仍然与初始化相同。

1 个答案:

答案 0 :(得分:2)

使用AutoCompletionBinding<String>。此应用演示如何使用AutoCompletionBinding<String>List<String>根据所选的RadioButton更改绑定。

import java.util.Arrays;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.controlsfx.control.textfield.AutoCompletionBinding;
import org.controlsfx.control.textfield.TextFields;

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

    AutoCompletionBinding<String> abcBinding;
    AutoCompletionBinding<String> defBinding;

    @Override
    public void start(Stage primaryStage)
    {

        List<String> list1 = Arrays.asList("one", "two", "three");
        List<String> list2 = Arrays.asList("four", "five", "six");

        TextField textfield = new TextField();

        VBox root = new VBox();

        RadioButton radioButton1 = new RadioButton("Left");
        RadioButton radioButton2 = new RadioButton("Right");

        ToggleGroup radioGroup = new ToggleGroup();
        radioButton1.setToggleGroup(radioGroup);
        radioButton2.setToggleGroup(radioGroup);
        radioGroup.selectedToggleProperty().addListener((obs, oldValue, newValue) -> {
            RadioButton tempRadioButton = (RadioButton) newValue;
            if (tempRadioButton == radioButton1) {
                if (defBinding != null) {
                    defBinding.dispose();
                }
                abcBinding = TextFields.bindAutoCompletion(textfield, list1);
            }
            else if (tempRadioButton == radioButton2) {
                if (abcBinding != null) {
                    abcBinding.dispose();
                }
                defBinding = TextFields.bindAutoCompletion(textfield, list2);
            }
        });

        HBox hbox = new HBox(radioButton1, radioButton2);

        root.getChildren().addAll(hbox, textfield);

        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);
    }

}