Header Ads

Create Employee Management System using JAVA - Part 11

How to create & use jmenubar, jmenuitem, jtoolbar, JSeparator in java














In this tutorial we discuss about how to add Menu bar into your program and when mouse point in over to menu it will appears menu items. You can easily drag and drop 'Menu Bar' in Swing Menus. Then add 'Tool Bar' from Swing Containers. You can rename the field and also make a short cut key for the action.

Add menu items: right click on menu bar --> Add From Palette --> Menu item. And also you can add a separator by using same method. Add From Palette --> Separator










How to set Close option to use as close the windows. Right click on it --> Events --> Action --> ActionPerformed (make sure to go to the correct source : private void jMenuItem1ActionPerformed) otherwise code will not work well. Then add 'close();' 

But this will not work because you need a method to execute close();


// METHOD
public void close(){
WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
}

You can also add a button to below the File Menu instead of menu item or with the menu item. Use the same method to validate it.







private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                  
// TODO add your handling code here:
close();
}

In next part we will discuss about 'Add image, icon, picture to jmanuitem , jbutton, jtoolitem’ by using Java. If you have any doubts about the part – 11 please don’t hesitate to ask from iTech Digest | Thank you! See you on part - 12

No comments

Thank you very much for your ideas!