How to insert link in Menubar?

Asked

Viewed 152 times

2

I have a window in a MenuBar with some Menu’s (such as Archive, View, Community and etc) and would like to add one last element to the MenuBar where, when clicked, it would open a window on a specific website.

The problem is, from what I’ve seen, I can’t add buttons to a MenuBar.
I would therefore like to know: is there any way to do this within a MenuBar?

If not possible, how else could I get a menu bar from a window similar (not necessarily the same style) to the image below?

Barra Menu

1 answer

2


To use the getHostServices() in an application with FXML you must pass Hostservices as a parameter to your controller, as this method can only be called in the main class (application class method).

In your controller you will have to declare a Hostservices type variable in this way:

public class SeuController implements Initializable {

    private HostServices host;

    // Deve ser público pois será chamado na classe principal
    public void setHostService(HostServices host){
        this.host = host;
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    // ...
    }

    // Método para abrir o browser padrão do usuário com o respectivo site
    @FXML
    public void irParaSite(ActionEvent event){
        host.showDocument("http://www.seusite.com");
    } 
}

In the main class you should make a slight modification:

@Override
public void start(Stage stage) throws Exception {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
    Parent root = (Parent) loader.load();

    // Passando o HostService para o controller  
    SeuController controller = loader.getController();
    controller.setHostService(getHostServices());

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

I use Scenebuilder to mount my FXML and here each menu within the menubar comes with an included menuitem.

<Menu fx:id="menu" mnemonicParsing="false" text="About Us">
<items>
    <MenuItem mnemonicParsing="false" onAction="#irParaSite" text="Action 1" />
</items>
</Menu>

Although the menu can receive onAction, only Menuitem performs the action when clicked. If you put onAction on both, clicking Menuitem will activate onAction from the Menu as well.

[EDIT - Workaround to put an action in the Menu instead of Menuitem]

This issue is an adaptation of the following answer (Author credits): https://stackoverflow.com/questions/10315774/javafx-2-0-activating-a-menu-like-a-menuitem

As the behavior you expect is not usual there is an elegant way to configure setOnMouseClick directly from the Menu (See in the documentation that there is no such method) and addEventHandler does not correctly capture mouse events.

Below we have a workaround that solves the problem:

Note: For the solution to work you must remove the current text from the menu, that is, the tag "text=aDaAba" should not exist in FXML. Otherwise the two texts will appear and the click event will not work.

Label menuLabel = new Label("nomeDaAba");
menuLabel.setOnMouseClicked(new EventHandler<Event>() {
    @Override
    public void handle(Event event) {
        host.showDocument("http://www.seusite.com");
    }
});
menu.setGraphic(menuLabel);

As you can see the event was placed on the Menu Label, and worked in the tests performed.

  • Ready, now I edited and I think is no longer ambiguous.

  • The answer is more complete now because I remembered that you use FXML, but if you were someone else you might post the solution the way I did before (without using FXML). It is good to highlight in your question whether you use fxml or not.

  • Thank you very much for your reply!

  • I tested the code and noticed that it opened two tabs each time I clicked on the Menuitem, so I formulated the hypothesis that the onAction Menu is activated when we click on one of its items. So, to test, I removed onAction from Menuitem and ran the code, confirming what I thought.

  • However, unfortunately this is not the result I wanted. Apparently, I will not be able to use a Menubar for this.

  • I already edited the question again to see how to get around this problem. If you can take a look, I am very grateful. Also, the lack of specification of the use of FXML is due to the attempt to get a greater number of answers, after all, if I manage to do what I want I’m happy already.

  • I put onAction on both to illustrate the possibility, but it was only to use 1. The behavior you want is not usual, so there is no "elegant" way to do this. I’m editing the answer with a workaround.

  • Gee! It worked perfectly. Thank you again.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.