2017-05-25 36 views
0

我有可編輯的組合框。什麼事件是從組合框中選擇項目,而不是actionevent?

tf_name.getEditor().setOnKeyReleased(event -> { 
     tf_name.hide(); 
     String name= tf_name.getEditor().getText().trim(); 
     tf_name.getSelectionModel().clearSelection(); 


     tf_name.getEditor().setText(name); 
     tf_name.setItems(Customer.getNameCompanyList(name)); 
     int size=Customer.getNameCompanyList(name).size(); 
     if (size>10) {size=10; 
      if (size==0) size=0; 
     } 
     tf_name.setVisibleRowCount(size); 
     if (size!=0) 
      tf_name.show(); 
     tf_name.getEditor().end(); 
     /*tv_info1.setItems(Info2.getInfo2NameDog(name));*/}); 
    tf_name.setOnAction( e->{ 
     { 

      Customer customer=Customer.getCustomer(Customer.find_id_cust_Name(tf_name.getValue())); 
      tf_phone.setText(customer.getPhone()); 
      cb_cust_district.setValue(customer.getDistrict()); 
      cb_city_type.setValue(customer.getCity_type()); 
      tf_city_name.setText(customer.getCity_name()); 
      tf_street.setText(customer.getStreet()); 
      tf_house.setText(String.valueOf(customer.getHouse())); 
      tf_housing.setText(customer.getHousing()); 
      tf_flat.setText(String.valueOf(customer.getFlat())); 
      tf_code.setText(String.valueOf(customer.getCode())); 
      tf_oplata_nomer.setText(String.valueOf(customer.getNomer_oplat())); 
     } 



    }); 

當我從組合框中選擇項目時,而不是當我鍵入組合框或其他時,我需要執行此操作。那麼我需要什麼actionevent,而不是setOnAction?

+0

嘗試添加監聽選擇模型的'selectedIndexProperty'? –

回答

0

您可以像改變監聽器添加到selectedItemProperty組合框的selectionModel設置下面的演示代碼:

ObservableList<String> options = FXCollections.observableArrayList(); 
     options.addAll("hello", "hi", "bye", "morning");  
     ComboBox<String> myComboBox = new ComboBox<String>();  
     myComboBox.setItems(options); 
     myComboBox.getSelectionModel().selectedItemProperty().addListener((Observable, oldValue, newValue) -> 
     System.out.println(newValue) 
       ); 
+0

歡迎來到StackOverflow並感謝您的幫助。你能用一些解釋來增加你的代碼唯一答案嗎? – Yunnosch

相關問題