Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javafx: setting a Tab mnemonics

Tags:

tabs

javafx

Quick question: Is it possible to set a mnemonic for a JavaFX Tab?

I can only seem to be able to set them for such controls as buttons, and menu items.

like image 782
Ricardo Magalhães Cruz Avatar asked Nov 13 '14 17:11

Ricardo Magalhães Cruz


1 Answers

Okay, this is an interesting question! You are right, you can not set the mnemonic directly on a Tab. But you can add a component as a Tabs graphic that supports the mnemonic feature:

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"));

Will make your tabs switchable via shortcuts.

like image 121
eckig Avatar answered Oct 20 '22 14:10

eckig