Right align menu in menubar in JavaFX

回眸只為那壹抹淺笑 提交于 2021-01-04 09:03:53

问题


In Java Swing it is possible to put a menu on the right side of the menu bar using:

menubar.add(menu1);
menubar.add(Box.createHorizontalGlue());
menubar.add(menu2);

This will put menu1 on the left and menu2 on the right. This function is (obviously) not available in JavaFX.

In JavaFX, I have seen that the same can be achieved for a toolbar using:

final Pane rightSpacer = new Pane();
HBox.setHgrow(
    rightSpacer,
    Priority.SOMETIMES
);

Although, this workaround is not usuable for menus.

Question: is there a way to create a right spacer for menus in JavaFX?


回答1:


One slightly-hacky way would be to use two menu bars in a HBox. You can give the spacer separating them the same style as a menu bar by adding the menu-bar style class to it:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.stage.Stage;

public class MenuAlignment extends Application {

    @Override
    public void start(Stage primaryStage) {
        MenuBar leftBar = new MenuBar();
        leftBar.getMenus().addAll(new Menu("File"), new Menu("Edit"));
        MenuBar rightBar = new MenuBar();
        rightBar.getMenus().addAll(new Menu("Help"));
        Region spacer = new Region();
        spacer.getStyleClass().add("menu-bar");
        HBox.setHgrow(spacer, Priority.SOMETIMES);
        HBox menubars = new HBox(leftBar, spacer, rightBar);

        BorderPane root = new BorderPane();
        root.setTop(menubars);
        Scene scene = new Scene(root, 600, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

A potential disadvantage of this approach is that you couldn't use this as a system menu bar.




回答2:


You can embed swing in javafx. An example of this would be

import javafx.application.Application;
import     javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javax.swing.JButton;
import javax.swing.SwingUtilities;

public class SwingFx extends Application {

    @Override
    public void start (Stage stage) {
        final SwingNode swingNode = new SwingNode();

        createSwingContent(swingNode);

        StackPane pane = new StackPane();
        pane.getChildren().add(swingNode);

        stage.setTitle("Swing in JavaFX");
        stage.setScene(new Scene(pane, 250, 150));
        stage.show();
        }

    private void createSwingContent(final SwingNode swingNode) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                swingNode.setContent(new JButton("Click me!"));
        }
    });
  }
}

so you would want to replace the JButton() with whatever you want to embed.

(The code snippet was taken from Oracles official documents)



来源:https://stackoverflow.com/questions/39281780/right-align-menu-in-menubar-in-javafx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!