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.
Which approach should I take here, having the book 'java swing' from oreilly on my desk but not finding anything that could help me.
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 ?
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
... do not want to use a static ...
for some reason.
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 ?
Would like to use your: code:
Just not fully understanding it
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|