2017-11-27 16 views
0

私はChoiceBoxを持っています。ユーザーが展開するたびに内容を更新したいと思います。私はこれに対して適切なリスナーを見つけられませんでした。 googleが与えるすべてのものは、ChangeValueイベントの処理に関連しています。Javafxは、選択ボックスのドロップダウンイベントリスナーを追加します

私が扱うのはChoiceBoxをクリックしているので、eventListener<ActionEvent>ChoiceBoxを追加する必要がありますが、私の実装は機能しません。

ListEventの値をクリックするとActionEventが発生し、ChoiceBoxをクリックした場合は発生しません。

答えて

1

選択ボックスのshowingPropertyでリスナーを登録します。ここでは

choiceBox.showingProperty().addListener((obs, wasShowing, isNowShowing) -> { 
    if (isNowShowing) { 
     // choice box popup is now displayed 
    } else { 
     // choice box popup is now hidden 
    } 
}); 

は、迅速なデモです:

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.ChoiceBox; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class ChoiceBoxPopupTest extends Application { 


    private int nextValue ; 

    @Override 
    public void start(Stage primaryStage) { 
     ChoiceBox<Integer> choiceBox = new ChoiceBox<>(); 
     choiceBox.getItems().add(nextValue); 
     choiceBox.setValue(nextValue); 
     choiceBox.showingProperty().addListener((obs, wasShowing, isNowShowing) -> { 
      if (isNowShowing) { 
       choiceBox.getItems().setAll(++nextValue, ++nextValue, ++nextValue); 
      } 
     }); 
     BorderPane root = new BorderPane(); 
     root.setTop(choiceBox); 
     BorderPane.setAlignment(choiceBox, Pos.CENTER); 
     root.setPadding(new Insets(5)); 
     Scene scene = new Scene(root, 400, 400); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
関連する問題