popup menus

G

google

I have a popup menu which shows itself when I right click over a
JTextArea called taLocations. I can use the menu with the following
ActionListener. Within the same frame I have another JTextArea called
taSkills which will utilise the same popup menu.

As you will see in the code I want to identify the name of the
JTextArea that the popup menu was "hovering" over. How?


ActionListener menuListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("ActionListener");
System.out.println(event.getActionCommand());

System.out.println(event.getSource().getClass().getName());
System.out.println(event.getClass().getName());
if (event.getActionCommand().equalsIgnoreCase("Save"))
{

}

if
(event.getActionCommand().equalsIgnoreCase("Restore")) {
System.out.println(event.getSource());
restoreFilter("locations");
}
}
};
 
R

Raymond DeCampo

I have a popup menu which shows itself when I right click over a
JTextArea called taLocations. I can use the menu with the following
ActionListener. Within the same frame I have another JTextArea called
taSkills which will utilise the same popup menu.

As you will see in the code I want to identify the name of the
JTextArea that the popup menu was "hovering" over. How?


ActionListener menuListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("ActionListener");
System.out.println(event.getActionCommand());

System.out.println(event.getSource().getClass().getName());
System.out.println(event.getClass().getName());
if (event.getActionCommand().equalsIgnoreCase("Save"))
{

}

if
(event.getActionCommand().equalsIgnoreCase("Restore")) {
System.out.println(event.getSource());
restoreFilter("locations");
}
}
};

There are many ways to do this. The most straight forward might be to
make your ActionListener a named class and add an attribute to hold the
name of the text area. Then when the menu pops, it is responsible for
setting the attribute. You could also have the attribute on the pop-up
menu object.

Alternatively you could use different instances of pop-up menu and
listeners that already know what text area they are associated to.

HTH,
Ray
 
G

google

Thanks. I've gone for the different instances of pop-up menu and
listeners. Like so -

ActionListener menuListenerSkills = new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equalsIgnoreCase("Save"))
{

}

if
(event.getActionCommand().equalsIgnoreCase("Restore")) {

}
}
};
itemSkills.addActionListener(menuListenerSkills);
popupSkills.setLabel("Justification");
popupSkills.setBorder(new BevelBorder(BevelBorder.RAISED));
taSkills.addMouseListener(new OpenSeatViewer().new
skillsMousePopupListener());


Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top