如何从下拉列表视图javafx 8中获取文本框中的单击项?

时间:2014-06-23 09:26:36

标签: java netbeans javafx-8

我想在我的文本框中点击值,我添加了监听器但没有得到调整输出。请建议我怎么做。

ObservableList<String> data5 = FXCollections.observableArrayList(smooth);
listView.setItems(data5);

listView.getSelectionModel().selectedItemProperty().addListener(
      new ChangeListener<String>() {
              public void changed(ObservableValue<? extends String> ov, 
                    String old_val, String new_val) 
                    {
                        System.out.println("********"+new_val);

                        txtCustomerName.textProperty();
                        txtCustomerName.setText(new_val);
                    }
        });

enter image description here

1 个答案:

答案 0 :(得分:2)

public class FillForm extends Application {

    Text addressOne;
    Text addressTwo;
    Text mobileOne;
    Text email;

    @Override
    public void start(Stage stage) throws Exception {

        Label companyNameLbl = new Label("Company Name");
        ComboBox<String> companyName = new ComboBox<String>();
        companyName.setEditable(true);

        populateCompanyName(companyName);
        addComboListener(companyName);

        HBox companyHbox = new HBox(25);
        companyHbox.getChildren().addAll(companyNameLbl, companyName);

        Label addressOneLbl = new Label("Address One");
        addressOne = new Text();

        HBox addressOneHbox = new HBox();
        addressOneHbox.getChildren().addAll(addressOneLbl, addressOne);

        Label addressTwoLbl = new Label("Address two");
        addressTwo = new Text();

        HBox addressTwoHbox = new HBox();
        addressTwoHbox.getChildren().addAll(addressTwoLbl, addressTwo);

        Label mobileLbl = new Label("Company Name");
        mobileOne = new Text();

        HBox mobileHbox = new HBox();
        mobileHbox.getChildren().addAll(mobileLbl, mobileOne);

        Label emailLbl = new Label("Company Name");
        email = new Text();

        HBox emailHbox = new HBox();
        emailHbox.getChildren().addAll(emailLbl, email);

        VBox form = new VBox(20);
        form.getChildren().addAll(companyHbox, addressOneHbox, addressTwoHbox,
                mobileHbox, emailHbox);

        Scene scene = new Scene(form);
        stage.setScene(scene);
        scene.getStylesheets().add("/comboStyles.css");
        stage.show();
    }

    private void addComboListener(final ComboBox<String> combo) {

        combo.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
                if (combo.getValue().equals("Apple")) {
                    addressOne.setText("\t Apple address one");
                    addressTwo.setText("\t Apple address two");
                    mobileOne.setText("\t Apple mobile number");
                    email.setText("\t Apple email");
                }

            }
        });
    }

    public void populateCompanyName(ComboBox<String> combo) {
        combo.getItems().add("Intel");
        combo.getItems().add("Apple");
        combo.getItems().add("Microsoft");
    }

    public static void main(String[] args) {
        launch(args);
    }
}

<强> comboStyles.css

.combo-box .arrow, .combo-box .arrow-button{
    -fx-background-color: transparent;
}

<强>输出:

enter image description here

enter image description here

我在没有使用适当布局的情况下做了一个粗略的例子。您可以为UI使用适当的布局。使用您的data5列表填充组合框,并在组合框的动作侦听器中检查所选值并填写其他字段。

<强>更新

由于您必须使用公司所说的文本框,因此以下链接将指导您。您必须通过扩展Javafx的Text类来创建自定义文本框。

https://github.com/privatejava/javafx-autocomplete-field

http://blog.ngopal.com.np/2011/07/04/autofill-textbox-with-filtermode-in-javafx-2-0-custom-control/

自动填充文字字段:

https://code.google.com/p/jfx-autocomplete-textfield/