JavaFX Menu - First Letter, Underline

Following the standards of the user interface: If an application menu item (in the top menu bar) opens a drop-down list, it MUST be decorated as shown below: Decorated menu item first letter The first letter has the property "text-decoration: underline". But given this http://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html I cannot do this in the javafx application.

Any suggestions?

+6
source share
1 answer

Mnemonic Background

What you indicate in your image is a mimic diagram of the keyboard . JavaFX does support mnemonics, but you will only see them on platforms that initially use mnemonics and where you programmed your application to use mnemonics. For example, Windows programs usually use mnemonics, so you will see mnemonics in your JavaFX application when you run the application on Windows, but OS X programs usually do not use mnemonics, so if you run the JavaFX application on OS X, you may not see the mnemonics displayed.

Also note that on Windows, even if you have defined your mnemonics as described above, you need to press the ALT key to see the Mnemonics underlined in the JavaFX application (standard Windows applications like Notepad work this way, so JavaFX is no different respect).

Define your mnemonics

Set the mnemonic parsing to true in the menu item. (In fact, this is indeed the default in the menu items, but read the related documentation to understand it a little better). In the text of the menu item, place the underscore _ in front of the letter that you would like to use as keyboard mnemonics for your menu item.

Everything Labeled (this is each control with text) can potentially display and respond to mnemonics, as long as you set the mnemonic parsing to true for the marked element and place an underscore in the label text.

Also identify boosters

Apple Developer Guidelines encourage the use of accelerators rather than mnemonics when writing applications for OS X. Thus, to make your application run better on a platform, I recommend delivering accelerators for your menu items, even if you already have mnemonics for use under Windows . You can setAccelerators in menu items in JavaFX. For more information about using accelerators in a JavaFX application, see Response to Using JavaFX 2.2 Mnemonics (and accelerators) (which actually demonstrates using accelerator, not mnemonic usage ...).

Use JavaFX CSS attributes, not w3c CSS attributes

The first letter has the text-decoration: underline property.

It does not matter and will not work in a JavaFX application. JavaFX supports different CSS properties than the w3c CSS attributes used in HTML development. In particular, JavaFX does not support: text-decoration:underline . JavaFX only supports CSS properties defined in the JavaFX CSS Reference. In particular, JavaFX Text supports -fx-underline to indicate that text should have an underline style applied to it. But even then, fx-underline will underline all the text in the label, and not a specific letter, which is not what you want.

Application example

Demonstrates the use of accelerators and mnemonics in menus in a JavaFX application. Notice how the example uses KeyCombination.keyCombination("SHORTCUT+N") to specify an accelerator shortcut. This is an OS-independent way to specify a standard OS shortcut in JavaFX. On Windows, SHORTCUT will be displayed on CTRL . In OS X, the SHORTCUT will be displayed in OS X COMMAND .

The screenshot shows the launch of the application in Windows 7 and pressing the ALT key to display the mnemonics.

New file sample

 import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.input.KeyCombination; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class MnemonicAcceleratedMenu extends Application { @Override public void start(Stage stage) { Menu fileMenu = new Menu("_File"); MenuItem newFileMenuItem = new MenuItem("_New..."); newFileMenuItem.setAccelerator( KeyCombination.keyCombination("SHORTCUT+N") ); newFileMenuItem.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Something new, this way comes"); } }); fileMenu.getItems().add( newFileMenuItem ); MenuBar menuBar = new MenuBar(); menuBar.getMenus().setAll( fileMenu ); VBox layout = new VBox(menuBar); layout.setPrefSize(200, 100); stage.setScene(new Scene(layout)); stage.show(); } public static void main(String[] args) { launch(args); } } 
+20
source

Source: https://habr.com/ru/post/971572/


All Articles