aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Which JButton is being pressed ? OK or Cancel ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Which JButton is being pressed ? OK or Cancel ?" Watch "Which JButton is being pressed ? OK or Cancel ?" New topic
Author

Which JButton is being pressed ? OK or Cancel ?

ing erl
Greenhorn

Joined: Jan 27, 2006
Posts: 15
Hello,

Did create a small example where the user
is prompted to add firstname and a lastname.
at the bottom are 2 buttons 'OK' and 'Cancel'.

Using the 2 Inner-Classes to detect whether the user
has clicked with the mouse ( using a class that implements ActionListener)
on either button - that is working fine - and testing if the user
has pressed 'enter' when either button is on-focus ( using a class that
implements KeyListener ) - but there I only detect if 'enter' has been pressed, and not knowing if 'OK' is pressed or if 'Cancel' is pressed.

Which approach should I take here, having the book 'java swing' from oreilly on my desk but not finding anything that could help me.

regards, i
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

You should be able to get the source of the KeyEvent and determine which button it is that way.


GenRocket - Experts at Building Test Data
ing erl
Greenhorn

Joined: Jan 27, 2006
Posts: 15
Hi!

Well creating an inner-class like this:

I only get to know if I have punched the 'enter'-key on the keyboard,
but I do not know if I have tabbed to the 'ok'-button or the
'cancel'-button.
So how do I detect if the 'ok'- or 'cancel'-button is in focus ?

someone said that I should use mnemonic .... but is that the right
way ?

regards, i
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by ing erl:
the user is prompted to add firstname and a lastname.
at the bottom are 2 buttons 'OK' and 'Cancel'.

Using the 2 Inner-Classes to detect whether the user
has clicked with the mouse ( using a class that implements ActionListener)
on either button - that is working fine - and testing if the user
has pressed 'enter' when either button is on-focus ( using a class that
implements KeyListener ) - but there I only detect if 'enter' has been pressed, and not knowing if 'OK' is pressed or if 'Cancel' is pressed.

It seems to me that anything with KeyListener is doing it the hard way. You can already 'press' a JButton from the keyboard using the SPACE key (this is actually up to the LnF, but SPACE is used by all LnFs I have seen), so does your code work to your satisfaction when the user gives focus to one of your buttons and presses SPACE?

If so, and you really want the user to be able trigger the button with the ENTER key, then all you need to do is add ENTER to each of your buttons' input maps:
You could also remove SPACE from the input maps, though I don't see why you would need to.

[edit: But to answer the precise question you asked, couldn't you just add one KeyListener to the OK button and another KeyListener to the Cancel button?]

Which approach should I take here, having the book 'java swing' from oreilly on my desk but not finding anything that could help me.

good choice of books

I used its Appendix B to look up the "pressed" and "released" strings that I used above.

`
[ December 04, 2007: Message edited by: Brian Cole ]

bitguru blog
ing erl
Greenhorn

Joined: Jan 27, 2006
Posts: 15
Thanks Brian,

Must check that out.
Did try your example, but where does the event fly ?
IF I would like to write out the 'ok' or 'cancel' in the console
after pressing, how do I do that ?

The KeyListener-interface gives me they below method:

And here I am able to check if Enter has been pressed by doing:

But still I do not know if the 'ok' is in focus or if 'cancel' is in focus.

Still I found the ability to fetch the source of this event by using the following:


which gives me the text written on the button

the lot would be like this:
public class MyNewListener implements KeyListener {
public void keyPressed(KeyEvent e) {
System.out.println("KeyPressed");
if (e.getKeyCode() == KeyEvent.VK_ENTER
&& e.getSource() instanceof AbstractButton)
{
System.out.println(" button "
+ ((AbstractButton) e.getSource()).getText()); }
}
}

this is working for me, but is this the best way to do things.
I am now able to create a 'MyButton' class that extends JButton and
adds a Listener that handles all my 'clicking'.
using this approach gives me some other stuff to deal with,
for instance from which OK button I am coming from when using
several Panes ( using TestingTabbedPane ) - but that could
ofcourse be solved using the listener above as an Inner-Class and
getting hold of the actual pane-title ... do not want to use a static ...
for some reason.

regards and thanks for your reply,
checking the appendix first thing in the morning.

regards, i
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by ing erl:
Did try your example, but where does the event fly ?
IF I would like to write out the 'ok' or 'cancel' in the console
after pressing, how do I do that ?


In the normal ActionListener you have already set for the buttons.
When the user uses the SPACE key (or ENTER after modifying the
input map as above) then it's a true button press that behaves
the same as if the user clicked the button with the mouse.


public class MyNewListener implements KeyListener {
public void keyPressed(KeyEvent e) {
System.out.println("KeyPressed");
if (e.getKeyCode() == KeyEvent.VK_ENTER
&& e.getSource() instanceof AbstractButton)
{
System.out.println(" button "
+ ((AbstractButton) e.getSource()).getText()); }
}
}

this is working for me, but is this the best way to do things.


I still don't see why you would use KeyListener at all, but if you're going to do so then I guess your code is fine.

Another approach (that I did mention above) is to use two instances of your listener. Something like:

MyNewListener okListener = new MyNewListener("string or int or something");
theOkButton.addXXXListener(okListener);

MyNewListener cancelListener = new MyNewListener("value2");
theCancelButton.addXXXListener(cancelListener);

This approach works with ActionListener or KeyListener.


I am now able to create a 'MyButton' class that extends JButton and
adds a Listener that handles all my 'clicking'.
using this approach gives me some other stuff to deal with


In this case it sounds like each button has its own listener intstance already, so I'm not sure why you're having problems. If you are trying to use getText() and buttons have the same text, you could instead call setActionCommand() with custom text for each one.

I still think it's easier to use the InputMap.


... do not want to use a static ...
for some reason.


I'm not sure what you're getting at here. If you don't want to add the same (static) listener interface to each button, then I can understand the apprehension. If you're trying to avoid declaring a nested class static then I don't. Static nested classes are cleaner than non-static ones, I think (unless they need to access the outer class's state, of course).
[ December 04, 2007: Message edited by: Brian Cole ]
ing erl
Greenhorn

Joined: Jan 27, 2006
Posts: 15
Hi!

Thanks.

Would like to use your:
code:



Just not fully understanding it, the fault is mine however.
I can see that this is the right way to do things.

are there any good examples to find regarding your example ?
let say I have four buttons / 'previous','next','first' and 'last'.
how do I write out the names of the button in a console ?

regards, i ( still morning, so, a cup of coffee might do the trick)
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by ing erl:
are there any good examples to find regarding your example ?
let say I have four buttons / 'previous','next','first' and 'last'.
how do I write out the names of the button in a console ?


In your original posting you said you could "detect whether the user
has clicked with the mouse ( using a class that implements ActionListener)
on either button - that is working fine". Later you showed you knew how to do ((AbstractButton) e.getSource()).getText(), so I'm not sure what you are asking.

It seems like you just put these two things together (with a call to System.out.println) and you're writing the names of the buttons to the console whenever the user clicks the mouse or presses SPACE when a button has focus.

If you add those two getInputMap().put() lines, then the same thing will happen whenever the user presses ENTER when a button has focus.

Before we go further let me flat-out ask you if you are able to do what you intend to do when the user clicks the buttons with the mouse. Are you? I have presumed so. If not, then we have basic issues to address before we get involved with ENTER keys and KeyListeners.

Would like to use your: code:
Just not fully understanding it


JButton already has a mapping from the SPACE key to the pressed/released actions in its ActionMap. All those two lines of code do is add similar mappings for the ENTER key. This is discussed in chapter 3 of your O'Reilly 'Java Swing' book. In particular, take a look at section 3.5.14: Keyboard Events.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Which JButton is being pressed ? OK or Cancel ?