I have a circular popup menu with a number of buttons which launches based on the location of the mouse on the screen. I am using Java 17 and JavaFx 22.0.1. The popup works well however my issue is the the pane is annoyingly still visible and noticable when you mouse down to the taskbar in windows. Here is the core elements of the code
@Override
public void initialize(URL location, ResourceBundle resources) {
popup = new Popup();
initializeCircularMenu();
}
}
private void hideMenu() {
if (popup != null) {
popup.hide();
}
// Hide using CSS visibility
circularMenuPane.setVisible(false);
circularMenuPane.setManaged(false);
// Ensure the pane is removed from its parent
StackPane stackPane = ui.getStackPane();
if (stackPane.getChildren().contains(circularMenuPane)) {
System.out.println("Removing circularMenuPane from parent: " + stackPane.getId());
System.out.println("Parent before removal: " + stackPane.getChildren());
stackPane.getChildren().remove(circularMenuPane);
stackPane.layout(); // Force layout pass
System.out.println("Parent after removal: " + stackPane.getChildren());
System.out.println("Is circularMenuPane in scene graph: " + stackPane.getChildren().contains(circularMenuPane));
}
}
public void showMenu(Double x, Double y, PrimaryController ui) {
this.ui = ui;
this.clickX = x;
this.clickY = y;
// Calculate the map coordinates
Point2D point = new Point2D(x, y);
Point mapPoint = ui.getMapView().screenToLocation(point);
Point wgs84Point = (Point) GeometryEngine.project(mapPoint, SpatialReferences.getWgs84());
coordinates = wgs84Point.getX() + "," + wgs84Point.getY();
int uniqueId = createUniqueCode();
// Set the size of the circularMenuPane
circularMenuPane.setPrefSize(RADIUS * 2, RADIUS * 2);
// Add the circularMenuPane to the stackPane if not already added
StackPane stackPane = ui.getStackPane();
if (!stackPane.getChildren().contains(circularMenuPane)) {
System.out.println("Adding circularMenuPane to the stackPane: " + stackPane.getId());
stackPane.getChildren().add(circularMenuPane);
}
circularMenuPane.setManaged(true);
circularMenuPane.setVisible(true);
// Set the popup content and show it
popup.getContent().clear();
popup.getContent().add(circularMenuPane);
popup.setAutoHide(false);
popup.show(ui.getPrimaryStage());
// Adjust the popup to be centered on the click coordinates
double paneCenterX = circularMenuPane.getPrefWidth() / 2;
double paneCenterY = circularMenuPane.getPrefHeight() / 2;
double offsetX = clickX - paneCenterX;
double offsetY = clickY - paneCenterY;
double additionalOffsetX = 45; // Based on the offset seen in previous results
double additionalOffsetY = -100; // Based on the offset seen in previous results
popup.setX(offsetX - additionalOffsetX);
popup.setY(offsetY - additionalOffsetY);
System.out.println("Click coordinates: (" + clickX + ", " + clickY + ")");
System.out.println("Popup set to: (" + popup.getX() + ", " + popup.getY() + ")");
System.out.println("Pane center: (" + paneCenterX + ", " + paneCenterY + ")");
popup.getContent().get(0).setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ESCAPE) {
hideMenu();
}
});
popup.getContent().get(0).requestFocus();
}
private void initializeCircularMenu() {
circularMenuPane = new Pane();
circularMenuPane.setId("circularMenuPane");
circularMenuPane.setPrefSize(RADIUS * 2, RADIUS * 2);
// Add a black circle at the center
Circle centerCircle = new Circle(RADIUS, RADIUS, 10, Color.BLACK);
circularMenuPane.getChildren().add(centerCircle);
// Add menu items with labels below the icon
for (int i = 0; i < NUM_ITEMS; i++) {
double angle = 2 * Math.PI / NUM_ITEMS * i;
double xButton = RADIUS + RADIUS * Math.cos(angle) - 50;
double yButton = RADIUS + RADIUS * Math.sin(angle) - 50;
Button button = new Button(buttonLabels[i]);
button.setShape(new Circle(10));
button.setMinSize(100, 100);
button.setMaxSize(100, 100);
button.setStyle("-fx-content-display: top; -fx-font-size: 10px;");
button.setWrapText(true);
button.setTextAlignment(javafx.scene.text.TextAlignment.CENTER);
String iconPath = getIconPath(i);
if (iconPath != null) {
ImageView icon = new ImageView(new Image(iconPath));
icon.setFitWidth(35);
icon.setFitHeight(35);
button.setGraphic(icon);
} else {
System.err.println("Icon not found for button: " + buttonLabels[i]);
}
button.setLayoutX(xButton);
button.setLayoutY(yButton);
final int index = i;
button.setOnAction(event -> handleButtonClick(index));
circularMenuPane.getChildren().add(button);
}
}
I have tried the following:
CSS:
.center-circle {
-fx-fill: black;
}
.button {
-fx-background-color: lightblue;
-fx-border-color: blue;
-fx-border-radius: 15px;
}
.pane {
-fx-background-color: transparent;
}
#circularMenuPane {
visibility: hidden;
opacity: 0;
}
#circularMenuPane.visible {
visibility: visible;
opacity: 1;
private void hideMenu() {
// Check and remove the circularMenuPane from its parent
Node parent = circularMenuPane.getParent();
if (parent != null) {
System.out.println("Removing circularMenuPane from parent: " + ((Pane) parent).getId());
((Pane) parent).getChildren().remove(circularMenuPane);
circularMenuPane.setVisible(false);
circularMenuPane.setManaged(false);
circularMenuPane.getChildren().clear();
System.out.println("Menu hidden and pane removed from parent");
// Force a refresh of the parent container
((Pane) parent).layout();
} else {
System.out.println("circularMenuPane has no parent");
}
popup.hide();
}
private void hideMenu() {
if (popup != null) {
popup.hide();
}
// Debugging output to trace parent and children
Pane parent = (Pane) circularMenuPane.getParent();
if (parent != null) {
System.out.println("Removing circularMenuPane from parent: " + parent.getId());
System.out.println("Parent before removal: " + parent.getChildren());
parent.getChildren().remove(circularMenuPane);
System.out.println("Parent after removal: " + parent.getChildren());
} else {
System.out.println("circularMenuPane parent is null, cannot remove.");
}
circularMenuPane.getChildren().clear();
circularMenuPane.setVisible(false);
circularMenuPane.setManaged(false);
// Ensure the UI is updated
circularMenuPane.getScene().getWindow().sizeToScene();
System.out.println("Menu hidden and pane removed from parent");
}`
```
New contributor
Rodney Bowden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.