-
Bug
-
Resolution: Fixed
-
P4
-
8u40
If a Menu is added to a MenuBar dynamically/later/at runtime/sometime after its creation, the MenuBarSkin will not update if the visibility of these added Menus changes.
The following test application:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
private Scene scene;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
MenuBar menuBar = new MenuBar();
menuBar.getMenus().add(new Menu("Before 1"));
menuBar.getMenus().add(new Menu("Before 2"));
Button addButton = new Button("Add new menu");
addButton.setOnAction((pActionEvent) -> {
menuBar.getMenus().add(new Menu("Added " + Integer.toString(menuBar.getMenus().size())));
});
ToggleButton trigger = new ToggleButton("Trigger Visibility");
trigger.setOnAction((pActionEvent) -> {
for (Menu menu: menuBar.getMenus())
{
menu.setVisible(!trigger.isSelected());
}
});
BorderPane root = new BorderPane();
root.setTop(menuBar);
root.setCenter(addButton);
root.setBottom(trigger);
root.setMinSize(640, 480);
scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX Menu Test");
primaryStage.show();
}
}
1. The MenuBar contains two items, pressing the visibility button does show and hide them.
2. Press the "Add" button to add a new Menu.
3. Pressing the visibility button does now hide the two original menus, but not the newly added one.
4. Pressing the visibility button again, the two original menus are now visible, but the newly added one is hidden.
From what I can see, the MenuBarSkin will only attach listeners to the visible property of the Menus in its constructor, so Menus that are added "later" will not trigger the rebuild of the UI.
The following test application:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
private Scene scene;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
MenuBar menuBar = new MenuBar();
menuBar.getMenus().add(new Menu("Before 1"));
menuBar.getMenus().add(new Menu("Before 2"));
Button addButton = new Button("Add new menu");
addButton.setOnAction((pActionEvent) -> {
menuBar.getMenus().add(new Menu("Added " + Integer.toString(menuBar.getMenus().size())));
});
ToggleButton trigger = new ToggleButton("Trigger Visibility");
trigger.setOnAction((pActionEvent) -> {
for (Menu menu: menuBar.getMenus())
{
menu.setVisible(!trigger.isSelected());
}
});
BorderPane root = new BorderPane();
root.setTop(menuBar);
root.setCenter(addButton);
root.setBottom(trigger);
root.setMinSize(640, 480);
scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX Menu Test");
primaryStage.show();
}
}
1. The MenuBar contains two items, pressing the visibility button does show and hide them.
2. Press the "Add" button to add a new Menu.
3. Pressing the visibility button does now hide the two original menus, but not the newly added one.
4. Pressing the visibility button again, the two original menus are now visible, but the newly added one is hidden.
From what I can see, the MenuBarSkin will only attach listeners to the visible property of the Menus in its constructor, so Menus that are added "later" will not trigger the rebuild of the UI.