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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With