Java Form Controls: 1 2 3 4 5 6 7
You can add menus to your Java forms, things like File, Edit, View, etc. Each menu has menu items, and these in turn can have sub menus.
You can start a new project for this, or just return to Design view and use the project you have. We'll use a new project.
In the NetBeans palette, locate the Menu Bar item, listed under Swing Menus:
Drag one to the top of your form. When you let the mouse button go, you'll have a default File and Edit menu bar:
There's no menu items added by default, though. To add your own, click on the File menu item to select it. With the File menu item selected, right-click. A new menu will appear. Select Add From Palette > Menu Item:
A Menu Item will be added to your File menu:
What we'll do is to add menu items to open and save a file.
Double click on the default text jMenuItem1. It will then be highlighted, so that you can type over it: (Or right-click and select Edit Text from the menu that appears.)
Type Open, then press the enter key on your keyboard:
Add another menu item in the way. This time, type Save as the menu item:
As you can see above, you can add shortcuts for your menu items. Click on to the Open menu item, then onto the shortcut for it:
With the shortcut item selected, have a look at the properties window:
Locate the Accelerator item, and click the small button to the right of the row. A dialogue box appears:
You can set which shortcut keys you want for a menu item from this dialogue box. An open shortcut is usually CTRL + O.
Hold down the CTRL on your keyboard and type an O in the Key Stroke box. Shift + O will appear in the text area:
Click OK, and the shortcut will be added to your Java menu item:
To see if all this works, click back on the Open menu item to highlight it. Now right click. From the menu that appears, select Events > Action > Action Performed. This will create a code stub for the menu item. Enter the following for the code:
javax.swing.JOptionPane.showMessageDialog( FormObjects.this, "Open" );
This is just a message box.
Run your program and try it out. Click File > Open and you should see the message box appear. Click OK to get rid of it. Now try your shortcut. Hold down the Ctrl key on your keyboard. Keep it held down and press the letter O. Again, the menu should appear.
Stop your program and return to your code stub. Comment out the message box. What we'll do is to write code to display a File Open dialogue box. A user can then select a file to be opened. We'll do that in the next part.
<-- Radio Buttons | File Open Dialogue Boxes -->