| Home: www.vipan.com | Vipan Singla | e-mail: vipan@vipan.com |
ButtonGroup onlyOne = new ButtonGroup(); onlyOne.add(firstButton); onlyOne.add(anotherButton);
| Restaurant | GUI |
|---|---|
| Hostess seats you on a table | JFrame mainWindow = new JFrame("Your Table");
|
| Waiter gives you a menu | JMenuBar menuBar = new JMenuBar();
|
| You point to a menu item and say "I'll take this" | User clicks on a menu item. |
mainWindow.enableEvents(AWTEvent.WINDOW_EVENT_MASK);
//In aseparate method, handle the event you are interested in
protected void processWindowEvent(WindowEvent e)
{
//Only handle the WINDOW_CLOSING event
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
mainWindow.dispose(); // Release resources
System.exit(0); // Exit the program
}
//Pass the rest of the events to super class (IMPORTANT!)
super.processWindowEvent(e); // Pass on the event
}
public void actionPerformed(ActionEvent e) {
Object source = e.getsource();
if (source == someMenuItem){ . . . }
. . .
}
mainWindow.addWindowListener(new WindowHandler()); // Add window listener
// Separately in main class, define an inner class to handle window events
// by extending WindowAdapter
class WindowHandler extends WindowAdapter
{
// Handler for window closing event. MAKE SURE THAT THE METHOD NAME IS SPELLED CORRECTLY
public void windowClosing(WindowEvent e)
{
mainWindow.dispose(); // Release the window resources
System.exit(0); // End the application
}
}
actionObject.putValue(NAME, "text name");
actionObject.putValue(SMALL_ICON, icon-object);
actionObject.putValue(SHORT_DESCRIPTION, "text description");
actionObject.putValue(LONG_DESCRIPTION, "more text description");
Icon lineIcon = (Icon)actionObject.getValue(SMALL_ICON);