Run attached code:
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author alexandr_kirov
*/
public class MenuOnShown extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
VBox vb = new VBox();
MenuBar mb = new MenuBar();
Menu menu = new Menu("Menu");
mb.getMenus().add(menu);
menu.getItems().clear();
for (int i = 0; i < 5; i++) {
menu.getItems().add(new MenuItem("MENU_ITEM" + i));
}
final CheckBox cb = new CheckBox("showing");
menu.showingProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
System.out.println("shown?");
cb.selectedProperty().setValue(t1);
}
});
vb.getChildren().addAll(cb, mb);
Scene scene = new Scene(vb, 300, 300);
stage.setScene(scene);
stage.show();
}
}
On launch, focus is on checkBox.
Press "Tab"
Press "Space"
you will see dropDown.
But this
menu.showingProperty().addListener(new ChangeListener<Boolean>() {
is not called.
Showing property is not reflect the currecnt state in such case.
But if you use mouse, and click on menu, than listener will be called.
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author alexandr_kirov
*/
public class MenuOnShown extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
VBox vb = new VBox();
MenuBar mb = new MenuBar();
Menu menu = new Menu("Menu");
mb.getMenus().add(menu);
menu.getItems().clear();
for (int i = 0; i < 5; i++) {
menu.getItems().add(new MenuItem("MENU_ITEM" + i));
}
final CheckBox cb = new CheckBox("showing");
menu.showingProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
System.out.println("shown?");
cb.selectedProperty().setValue(t1);
}
});
vb.getChildren().addAll(cb, mb);
Scene scene = new Scene(vb, 300, 300);
stage.setScene(scene);
stage.show();
}
}
On launch, focus is on checkBox.
Press "Tab"
Press "Space"
you will see dropDown.
But this
menu.showingProperty().addListener(new ChangeListener<Boolean>() {
is not called.
Showing property is not reflect the currecnt state in such case.
But if you use mouse, and click on menu, than listener will be called.