Our Notepad is sufficient with functionalities we have added. But I can still feel there is something missing. Something that we should add to our notepad to make it complete. What about adding the Mnemonics? What if our user presses the combination of ALT button and a specified Key and the File menu opens up automatically. Yes, this is definitely required.
How to achieve this? Is is complicated? Well, no. As a matter of fact this is very easy. Open you notepad.fxml and update the following.
<Menu text="_File" fx:id="menufile" mnemonicParsing="true">
<Menu text="_Edit" mnemonicParsing="true">
<Menu text="F_ormat" mnemonicParsing="true">
<Menu text="_View" mnemonicParsing="true">
<Menu text="_Help" mnemonicParsing="true">
Looks pretty simple right? All that you have to do is add mnemonicParsing="true" wherever required. Now, for the key that you would want to associate with the ALT key all that you have to do is put an "_" before that letter in your Menu text as shown above. For example if you want your File Menu to be opened up when you press ALT + F then change the text for File Menu from "File" to "_File". If you want Format Menu to be opened on ALT+O then change "Format" to "F_ormat".
That's it. The Mnemonics have been set. Now, run your application and test.
While the Mnemonics have been set and are paying a very important user friendly activity for us, but we still need a little more than this. There are some additional features that a notepad should have for its full usage. What are these? Lets say you are typing on the notepad and you feel like saving your work. Normally you'll do a Ctrl+S and it works for you. But here we have not provided anything that helps with this. Lets try and add this feature to our notepad as well. Now this may sound tricky but trust me it's not. This can easily be achieved through Accelerators.
Let's start with this. Open your fxml file. Now, wherever you want an accelerator defined update you code as shown below.
<MenuItem fx:id="newItem" text="New" onAction="#newFile"></MenuItem>
All you are doing here is adding an ID to your Menu Item.
Now open you controller class and update it so that it implements the "javafx.fxml.Initializable" interface. The method that you'll have to implement would be
@Override
public void initialize(URL location, ResourceBundle resources) {
}
Simple. This is called before the fxml is loaded and hence used to initialize your screen.
Now add a global parameter that defines the menu item you have given ID's to.
@FXML
private MenuItem newItem;
So far so good. Now you have got the reference of your MenuItem. For me it is the New MenuItem in FileMenu.
Now, add the following code in your initialize method.
KeyCombination kc = new KeyCodeCombination(KeyCode.N,
KeyCombination.CONTROL_DOWN);
newItem.setAccelerator(kc);
The above code declares a KeyCombination class. Here you define the key combinations. In the example above you have added Ctrl + N as Key Combination. next you add the combination as an accelerator to the menuitem instance.
That's it. Now Ctrl +N will also call the onAction method defined on New Menu Item.
Your shortcut is ready. These definitely are a lot helpful in using your applications and hence should be used wherever you can.
Notepad is finished. There were a lot of things that could have been done here but I did not because it was not worth discussing here. For example, adding a message box when you press New in File Menu asking the user if he actually wants to save his work or not. I did not add the functionality of any other Menu like Edit. Trust me these are essentials for out application but need not be discussed here. The motive here was to examine the ease with which the applications like notepad be developed using JavaFX.
If you want to visit my prev blogs on notepad, please click on the links below.
Create a Notepad with Javafx2 - Building the Logic
Create a Notepad with Javafx2 - Styling
Create a Notepad with Javafx2 - The Layout
How to achieve this? Is is complicated? Well, no. As a matter of fact this is very easy. Open you notepad.fxml and update the following.
<Menu text="_File" fx:id="menufile" mnemonicParsing="true">
<Menu text="_Edit" mnemonicParsing="true">
<Menu text="F_ormat" mnemonicParsing="true">
<Menu text="_View" mnemonicParsing="true">
<Menu text="_Help" mnemonicParsing="true">
Looks pretty simple right? All that you have to do is add mnemonicParsing="true" wherever required. Now, for the key that you would want to associate with the ALT key all that you have to do is put an "_" before that letter in your Menu text as shown above. For example if you want your File Menu to be opened up when you press ALT + F then change the text for File Menu from "File" to "_File". If you want Format Menu to be opened on ALT+O then change "Format" to "F_ormat".
That's it. The Mnemonics have been set. Now, run your application and test.
While the Mnemonics have been set and are paying a very important user friendly activity for us, but we still need a little more than this. There are some additional features that a notepad should have for its full usage. What are these? Lets say you are typing on the notepad and you feel like saving your work. Normally you'll do a Ctrl+S and it works for you. But here we have not provided anything that helps with this. Lets try and add this feature to our notepad as well. Now this may sound tricky but trust me it's not. This can easily be achieved through Accelerators.
Let's start with this. Open your fxml file. Now, wherever you want an accelerator defined update you code as shown below.
<MenuItem fx:id="newItem" text="New" onAction="#newFile"></MenuItem>
All you are doing here is adding an ID to your Menu Item.
Now open you controller class and update it so that it implements the "javafx.fxml.Initializable" interface. The method that you'll have to implement would be
@Override
public void initialize(URL location, ResourceBundle resources) {
}
Simple. This is called before the fxml is loaded and hence used to initialize your screen.
Now add a global parameter that defines the menu item you have given ID's to.
@FXML
private MenuItem newItem;
So far so good. Now you have got the reference of your MenuItem. For me it is the New MenuItem in FileMenu.
Now, add the following code in your initialize method.
KeyCombination kc = new KeyCodeCombination(KeyCode.N,
KeyCombination.CONTROL_DOWN);
newItem.setAccelerator(kc);
The above code declares a KeyCombination class. Here you define the key combinations. In the example above you have added Ctrl + N as Key Combination. next you add the combination as an accelerator to the menuitem instance.
That's it. Now Ctrl +N will also call the onAction method defined on New Menu Item.
Your shortcut is ready. These definitely are a lot helpful in using your applications and hence should be used wherever you can.
Notepad is finished. There were a lot of things that could have been done here but I did not because it was not worth discussing here. For example, adding a message box when you press New in File Menu asking the user if he actually wants to save his work or not. I did not add the functionality of any other Menu like Edit. Trust me these are essentials for out application but need not be discussed here. The motive here was to examine the ease with which the applications like notepad be developed using JavaFX.
If you want to visit my prev blogs on notepad, please click on the links below.
Create a Notepad with Javafx2 - Building the Logic
Create a Notepad with Javafx2 - Styling
Create a Notepad with Javafx2 - The Layout