How to get a JTextField event that started a pop up menu in Java ?
- by Frank
I have a JTextField that represents a day, such as "Sunday", when I left mouse click on it, it changes background color, when I right mouse click on it, a pop up menu comes up, after I click on a menu item, such as "1st of month", it gets the value, closes the menu and then change the bgcolor, my code looks like this :
JTextField dayHeading=new JTextField("Su");
......
final JPopupMenu Pmenu;
JMenuItem menuItem;
Pmenu=new JPopupMenu();
menuItem=new JMenuItem("1st of month");
Pmenu.add(menuItem);
menuItem=new JMenuItem("2nd of month");
Pmenu.add(menuItem);
......
menuItem.addMouseListener(new MouseAdapter()
{
public void mouseReleased(final MouseEvent e)
{
System.out.println(((JMenuItem)e.getComponent()).getText());
onHeadingClicked(e); // Error : java.lang.ClassCastException: javax.swing.JMenuItem cannot be cast to javax.swing.JTextField
// How to get the orininal JTextField event that started this pop up menu, so I can pass it onto onHeadingClicked() ?
}
});
......
dayHeading.setEditable(false);
dayHeading.setFocusable(false);
dayHeading.addMouseListener(new MouseAdapter()
{
public void mouseReleased(final MouseEvent evt)
{
if (SwingUtilities.isLeftMouseButton(evt)) onHeadingClicked(evt);
else if (SwingUtilities.isRightMouseButton(evt)) Pmenu.show(evt.getComponent(),evt.getX(),evt.getY());
}
});
......
void onHeadingClicked(final java.awt.event.MouseEvent evt)
{
final javax.swing.JTextField fld=(javax.swing.JTextField) evt.getSource();
...
}
My question is : in the menuItem.addMouseListener section, how to get the orininal JTextField event that started this pop up menu, so I can pass it onto onHeadingClicked() ?