15

I know that we can enable global menu for Java Swing applications using Jayatana. But I am searching for global menu support for JavaFX applications. After searching on it, I have found that setUseSystemMenuBar is used in JavaFX to enable same kind of behavior in Mac. But it is not working in Ubuntu.

My Java code:

import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.stage.*;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 300, 250, Color.WHITE);

        MenuBar menuBar = new MenuBar();
        // Use system menu bar
        menuBar.setUseSystemMenuBar(true);
        menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
        root.setTop(menuBar);

        Menu fileMenu = new Menu("File");

        Menu webMenu = new Menu("Web");

        Menu sqlMenu = new Menu("SQL");

        menuBar.getMenus().addAll(fileMenu, webMenu, sqlMenu);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Output:

JavaFX Menu in Ubuntu

Also there is a bug report regarding this problem here: Gtk: Implement global system menu bar support

Are there any workarounds to solve this problem?

2
  • Have you found out yet?
    – Socrates
    Oct 14, 2016 at 2:01
  • @Socrates not yet.
    – Gobinath
    Oct 14, 2016 at 2:51

2 Answers 2

2

Looking at the code, and I'm not an experienced FX developer, but it looks like the line "root.setTop(menuBar);" is locating the menu bar at the top of your container, not in the top row of the total display. I think some other command is necessary. I only have Java AWT and Swing experience and may be wrong.

0

If not solved already. You can try using separate fxml file for designing the UI. Use scene builder for the same. Scene builder, download the same from here.

I've tried the same in ubuntu 16.04 and it works fine. Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.