Javafx: setting the Mnemonics tab

Quick question: is it possible to set mnemonics for the JavaFX tab?

It seems to me that I can only set them for controls such as buttons and menu items.

+4
source share
1 answer

Ok, this is an interesting question! You are right, you cannot set the mnemonics directly on the tab. But you can add a component as a tab graph that supports mnemonics:

private class MTab extends Tab
{
    public MTab(String pText)
    {
        super();
        Button fakeLabel = new Button(pText);
        fakeLabel.setMnemonicParsing(true);
        fakeLabel.getStyleClass().clear();
        setGraphic(fakeLabel);
        fakeLabel.setOnAction(ev -> {
            if (getTabPane() != null) {
                getTabPane().getSelectionModel().select(this);
            }
        });
    }
}

Using this tab:

TabPane tabs = new TabPane();
tabs.getTabs().add(new MTab("_this is a test"));
tabs.getTabs().add(new MTab("t_his is a test"));
tabs.getTabs().add(new MTab("th_is is a test"));

Allows you to switch using shortcuts.

+3
source

All Articles