How to insert link in MenuBar?

2

I have a window in a MenuBar with some Menu's (like File, View, Community and etc) and would like to add a last element to MenuBar , which when clicked would open a window on a specific site .

The problem is that, from what I saw, I can not add buttons in a MenuBar . I would then like to know: is there any way to do this within MenuBar ?

If it is not possible, how else could I get a menu bar of a similar window (not necessarily the same style) with the one in the image below?

    
asked by anonymous 19.07.2017 / 03:31

1 answer

2

To use getHostServices () in an application with FXML you should 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 variable of type HostServices like this:

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 every menu inside the menubar comes with a MenuItem included.

<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 the MenuItem performs the action when it is clicked. If you put onAction in both, clicking the MenuItem will activate Menu onAction as well.

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

This edition is an adaptation of the following answer (Credits to the author): link

As the behavior you expect is unusual, there is no elegant way to set the setOnMouseClick directly from the Menu (see documentation that there is no such method), and the addEventHandler does not correctly capture mouse events.

Below is a workaround that solves the problem:

Note: For the solution to work, you should remove the current text from the menu, ie the "text = name" tag 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 in the Menu Label, and it worked in the tests performed.

    
19.07.2017 / 21:41