Setting the visible property of a SeperatorMenuItem to false does not hide it.
Fortunately, SeperatorMenuItem.getContent().setVisible(false) achieves the desired behavior, but one would expect SeparatorMenuItem.setVisible(false) to do the same.
Here's a sample application that demonstrates this issue:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SeparatorMenuItemTest extends Application {
public SeparatorMenuItemTest() {
}
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(final Stage stage) {
MenuItem menuItemA = new MenuItem("Menu Item A");
final SeparatorMenuItem separatorMenuItem = new SeparatorMenuItem();
MenuItem menuItemB = new MenuItem("Menu Item B");
final ContextMenu contextMenu = new ContextMenu(menuItemA, separatorMenuItem, menuItemB);
Button toggleSeparatorBtn = new Button("Toggle SeparatorMenuItem Visibility");
toggleSeparatorBtn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent ae) {
separatorMenuItem.setVisible(!(separatorMenuItem.isVisible())); //this does nothing on the UI
//Workaround:
// separatorMenuItem.getContent().setVisible(!(separatorMenuItem.getContent().isVisible()));
}
});
StackPane stackPane = new StackPane(toggleSeparatorBtn);
stackPane.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
if (me.getButton() == MouseButton.SECONDARY) {
contextMenu.show(stage, me.getScreenX(), me.getScreenY());
}
}
});
Scene scene = new Scene(stackPane);
stage.setScene(scene);
stage.setWidth(500);
stage.setHeight(500);
stage.show();
}
}
Fortunately, SeperatorMenuItem.getContent().setVisible(false) achieves the desired behavior, but one would expect SeparatorMenuItem.setVisible(false) to do the same.
Here's a sample application that demonstrates this issue:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SeparatorMenuItemTest extends Application {
public SeparatorMenuItemTest() {
}
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(final Stage stage) {
MenuItem menuItemA = new MenuItem("Menu Item A");
final SeparatorMenuItem separatorMenuItem = new SeparatorMenuItem();
MenuItem menuItemB = new MenuItem("Menu Item B");
final ContextMenu contextMenu = new ContextMenu(menuItemA, separatorMenuItem, menuItemB);
Button toggleSeparatorBtn = new Button("Toggle SeparatorMenuItem Visibility");
toggleSeparatorBtn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent ae) {
separatorMenuItem.setVisible(!(separatorMenuItem.isVisible())); //this does nothing on the UI
//Workaround:
// separatorMenuItem.getContent().setVisible(!(separatorMenuItem.getContent().isVisible()));
}
});
StackPane stackPane = new StackPane(toggleSeparatorBtn);
stackPane.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
if (me.getButton() == MouseButton.SECONDARY) {
contextMenu.show(stage, me.getScreenX(), me.getScreenY());
}
}
});
Scene scene = new Scene(stackPane);
stage.setScene(scene);
stage.setWidth(500);
stage.setHeight(500);
stage.show();
}
}