how to activate a menuitem on popup menu?

hello there

i've made a popup menu with to menu items

PopupMenu popup =new PopupMenu();

MenuItem defaultItem2 =new MenuItem("Restore");

MenuItem defaultItem =new MenuItem("Exit");

popup.add(defaultItem2);

popup.add(defaultItem);

and i've put the popup menu on a tray

class TrayActionListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent e){

trayIcon.getPopupMenu();

}

};

and i want if the user selects the Exit menuitem the program is closed

and if he chooses restore the frame is restored from the system tray

how to do these two action?

[1056 byte] By [First_knighta] at [2007-11-27 8:12:45]
# 1

Not sure what your TrayActionListener is supposed to do, but to add behavior to the menu items you would add action listeners to them, e.g.,defaultItem.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// your code here

}

});

For the restore action it depends on what you mean, e.g., if you mean show the hidden application use frame.setVisible(true).

For the exit action use frame.dispose().

dwga at 2007-7-12 19:57:10 > top of Java-index,Java Essentials,New To Java...
# 2

thanks dwg but i have a little problem here

i have 2 classes one for the systemtray and other for actions on it

each class is independent

public void setupSystemTray() {

if (SystemTray.isSupported()) {

SystemTray tray = SystemTray.getSystemTray();

Image image = Toolkit.getDefaultToolkit().getImage("D:\\Other\\JAVA\\Icons\\shutdown.png");

PopupMenu popup = new PopupMenu();

MenuItem defaultItem2 = new MenuItem("Restore");

MenuItem defaultItem = new MenuItem("Exit");

defaultItem.addActionListener(this);

popup.add(defaultItem2);

popup.add(defaultItem);

trayIcon = new TrayIcon(image, "JShutdown Timer",popup);

trayIcon.setImageAutoSize(true);

trayIcon.addActionListener(new TrayActionListener());

try {

tray.add(trayIcon);

} catch (AWTException e) {

System.err.println("TrayIcon could not be added.");

}

}

}

class TrayActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

trayIcon.getPopupMenu();

if(e.getSource()==defaultItem)

{

System.exit(0);

}

}

};

and i want if the user chooses the exit menuitem the program exits

so i write

if(e.getSource()==defaultItem)

{

System.exit(0);

}

in the second class the IDE didn't recognize the variable defaultItem

and it gave me error couldn't find simple variable

how to fix that?

First_knighta at 2007-7-12 19:57:10 > top of Java-index,Java Essentials,New To Java...