galneweinhaw
asked on
How to single-out a right-click for a JMenuItem
currently, my menu item has the following:
JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem("...");
menuItem.setSelected(....i sEnabled() );
menuItem.addActionListener (new ActionListener() {
public void actionPerformed(ActionEven t e) {
new mYDialog(....).setVisible( true);
}
}
/*
however, on a left-click I would like to toggle the checkbox of the menu item. I would like to keep all the other actions as normal....left-click opens menu, Enter opens menu etc.
Thanks!
JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem("...");
menuItem.setSelected(....i
menuItem.addActionListener
public void actionPerformed(ActionEven
new mYDialog(....).setVisible(
}
}
/*
however, on a left-click I would like to toggle the checkbox of the menu item. I would like to keep all the other actions as normal....left-click opens menu, Enter opens menu etc.
Thanks!
ASKER
How?
I would think
if (e ==?)
but from the info on ActionEvents:
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ActionEvent.html
I can't see what I need =(
I would think
if (e ==?)
but from the info on ActionEvents:
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ActionEvent.html
I can't see what I need =(
ASKER
I was going to use paramString() but that says it's for debgging only. I don't see how else to do it though.
sorry, I was wrong.
See : isRightMouseButton(MouseEv ent anEvent);
;JOOP!
See : isRightMouseButton(MouseEv
;JOOP!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
how about
if(e.getID() == Event.MOUSE_DOWN)
???
if(e.getID() == Event.MOUSE_DOWN)
???
ASKER
looking at something like this:
public void actionPerformed(ActionEven t e) {
if(e.getID() == Event.MOUSE_DOWN) {
menuItem.setSelected(menuI tem.isSele cted()? false : true );
how do I get access to the menuItem from inside actionPerformed?
public void actionPerformed(ActionEven
if(e.getID() == Event.MOUSE_DOWN) {
menuItem.setSelected(menuI
how do I get access to the menuItem from inside actionPerformed?
ASKER
"To be combined with the 'actionPerformed()' that knows which screen button / menu item it was"
and how do I do this?
I can call
menuItem.setSelected(menuI tem.isSele cted()? false : true );
inside actionPerformed.
and how do I do this?
I can call
menuItem.setSelected(menuI
inside actionPerformed.
try:
JCheckBoxMenuItem item = (JCheckBoxMenuItem) event.getSource();
JCheckBoxMenuItem item = (JCheckBoxMenuItem) event.getSource();
>>> how do I get access to the menuItem from inside actionPerformed?
I adapted the design pattern to create a GUI class per program:
public class MyGui extends Thread implements ActionListener
{
......................
private JButton b;
......................
void createGui()
{
b = new JButton("Press");
b.addActionListener(this);
.......................... ...
public void actionPerformed(ActionEven t e)
{
Object o = e.getSource();
if(o == b)
{
// .... and from here I have access to all components and variables.
;JOOP!
I adapted the design pattern to create a GUI class per program:
public class MyGui extends Thread implements ActionListener
{
......................
private JButton b;
......................
void createGui()
{
b = new JButton("Press");
b.addActionListener(this);
..........................
public void actionPerformed(ActionEven
{
Object o = e.getSource();
if(o == b)
{
// .... and from here I have access to all components and variables.
;JOOP!
Ahhhh, you can not even have a coffee-break in this forum!!
;JOOP!
;JOOP!
P.S.: the 'extends Thread' is there to even incorporate timers and asynchroneous processes
into the context. Please ignore that topic.
The pattern idea is: make your class listener to everything you are interested in,
so you can access all your administration from within.
Anonymous classes and separate listener classes have the problem you mentioned: proper access.
;JOOP!
into the context. Please ignore that topic.
The pattern idea is: make your class listener to everything you are interested in,
so you can access all your administration from within.
Anonymous classes and separate listener classes have the problem you mentioned: proper access.
;JOOP!
ASKER
public void actionPerformed(ActionEven t e) {
if (e.getID() == Event.MOUSE_DOWN){
JCheckBoxMenuItem item = (JCheckBoxMenuItem) e.getSource();
item.setSelected(item.isSe lected()? false: true);
}
ok
e.GetID() is giving 501
Event.MOUSE_DOWN is giving 1001
??
if (e.getID() == Event.MOUSE_DOWN){
JCheckBoxMenuItem item = (JCheckBoxMenuItem) e.getSource();
item.setSelected(item.isSe
}
ok
e.GetID() is giving 501
Event.MOUSE_DOWN is giving 1001
??
ASKER
oops, other way around:
e.GetID() is giving 1001
Event.MOUSE_DOWN is giving 501
even if I change it to:
public void actionPerformed(ActionEven t e) {
if (e.getID() == 1001){
JCheckBoxMenuItem item = (JCheckBoxMenuItem) e.getSource();
item.setSelected(item.isSe lected()? false: true);
}
the check box doesn't toggle =( nothing happens at all
e.GetID() is giving 1001
Event.MOUSE_DOWN is giving 501
even if I change it to:
public void actionPerformed(ActionEven
if (e.getID() == 1001){
JCheckBoxMenuItem item = (JCheckBoxMenuItem) e.getSource();
item.setSelected(item.isSe
}
the check box doesn't toggle =( nothing happens at all
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And a remark:
public void actionPerformed(ActionEven t e) {
if (e.getID() == 1001){
JCheckBoxMenuItem item = (JCheckBoxMenuItem) e.getSource();
item.setSelected(item.isSe lected()? false: true);
}
Did you check that e.getSource() really obtained the right control (the checkbox)? No, you assumed it.
Always say:
Object o = e.getSource();
if(o == <mycontrol>)
{
// Then it is from that control.
;JOOP!
public void actionPerformed(ActionEven
if (e.getID() == 1001){
JCheckBoxMenuItem item = (JCheckBoxMenuItem) e.getSource();
item.setSelected(item.isSe
}
Did you check that e.getSource() really obtained the right control (the checkbox)? No, you assumed it.
Always say:
Object o = e.getSource();
if(o == <mycontrol>)
{
// Then it is from that control.
;JOOP!
ASKER
thanks sciuriware, I will do.
question tho, what's the point of GetID if it will always return 1001?
(I was just using the hard coded number to check the rest of the code =) which helped, cus I got the check box thing working)
now I just need it only on left-click. Which will happen when I try your solution!
question tho, what's the point of GetID if it will always return 1001?
(I was just using the hard coded number to check the rest of the code =) which helped, cus I got the check box thing working)
now I just need it only on left-click. Which will happen when I try your solution!
ASKER
if(o == <mycontrol>)
how do I know what <mycontrol> is? the whole point of getting it .getSource is so it tells me what control it is...isn't it?
anyway, that part is working now =)
how do I know what <mycontrol> is? the whole point of getting it .getSource is so it tells me what control it is...isn't it?
anyway, that part is working now =)
If you want to check things like these, usr a visual debugger (ECLIPSE) or print it out to System.out.println();
The use of getId()? I searched the JAVA sources and I found that you should not call getId() in actionPerformed()
as it will always give this PRO FORMA value.
You should use the methods of ActionEvent() to get more info.
;JOOP!
The use of getId()? I searched the JAVA sources and I found that you should not call getId() in actionPerformed()
as it will always give this PRO FORMA value.
You should use the methods of ActionEvent() to get more info.
;JOOP!
if(o == .....)
YES! that's the way to find out what control is firing!!!!!!!!! see a snippet of one of my sources:
/** (non-Javadoc)
* @see java.awt.event.ActionListe ner#action Performed( java.awt.e vent.Actio nEvent)
* @param a action event.
*/
public void actionPerformed(ActionEven t a)
{
Object o = a.getSource();
if(o == abnButton)
{
ChiThuHelp.setHelp(ChiThuH elp.HELP_A BN);
activatePane(abnPane, CARD_ABN);
}
else if(o == ccdButton)
{
ChiThuHelp.setHelp(ChiThuH elp.HELP_C CD);
activatePane(ccdPane, CARD_CCD);
}
else if(o == corrButton)
{
YES! that's the way to find out what control is firing!!!!!!!!! see a snippet of one of my sources:
/** (non-Javadoc)
* @see java.awt.event.ActionListe
* @param a action event.
*/
public void actionPerformed(ActionEven
{
Object o = a.getSource();
if(o == abnButton)
{
ChiThuHelp.setHelp(ChiThuH
activatePane(abnPane, CARD_ABN);
}
else if(o == ccdButton)
{
ChiThuHelp.setHelp(ChiThuH
activatePane(ccdPane, CARD_CCD);
}
else if(o == corrButton)
{
ASKER
so...
JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(vision.g etLabel()) ;
menuItem.setSelected(visio n.isEnable d());
menuItem.addMouseListener( new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
if(SwingUtilities.isRightM ouseButton (me)) {
// reverse whatever the current enablement is
vision.setEnabled(!vision. isEnabled( ));
}
}
});
When debugging, this doesn't even go into mouseClicked, when I click the item, is there something wrong with the way I used new MouseAdapter?
JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(vision.g
menuItem.setSelected(visio
menuItem.addMouseListener(
public void mouseClicked(MouseEvent me) {
if(SwingUtilities.isRightM
// reverse whatever the current enablement is
vision.setEnabled(!vision.
}
}
});
When debugging, this doesn't even go into mouseClicked, when I click the item, is there something wrong with the way I used new MouseAdapter?
ASKER
by changing mouseClicked to mousePressed..it now works!
public void mousePressed(MouseEvent e) {
..any idea why?
public void mousePressed(MouseEvent e) {
..any idea why?
Do not wonder why the component (here the menuItem) chooses to call a
certain method on the listener.
I built some custom components with listeners and sometimes you don't
know to choose.
That's easier with ActionListener: only one callback: actionPerformed().
So, now you're done?
;JOOP!
certain method on the listener.
I built some custom components with listeners and sometimes you don't
know to choose.
That's easier with ActionListener: only one callback: actionPerformed().
So, now you're done?
;JOOP!
ASKER
yes, thanks a ton for your help!
Thanks to you too, I learned from this too, because I had to recover my knowledge.
;JOOP!
;JOOP!
;JOOP!