The Artima Developer Community
Sponsored Link

Java Answers Forum
A JButton with Double Listener

5 replies on 1 page. Most recent reply: May 3, 2005 10:42 AM by Collin Garymore

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

A JButton with Double Listener Posted: Apr 15, 2005 6:49 AM
Reply to this message Reply
Advertisement
Hi every Java GURU,
I'm doing a project and would like some Help please.
I would like to know How to Register TWO Listener (ActionListener & KeyListener) to a JButton implemented in a round Shape, be able to use the Keyboard aswell as the Mouse.
        /**
         *
         */
        key_Q = new RoundButton ( "Q" );
        key_Q.setBackground ( Color.darkGray );
        key_Q.setForeground ( Color.white );
        key_Q.setFont ( key_Label_Font );
 
        /**
         * Adds an ActionListener to the button.
         * Adds a KeyListener to the button.
         * @param 
         */
        key_Q.addKeyListener ( key_Handler );
        key_Q.addActionListener ( myAct );

- RoundButton: is a class that reshaping the normal JButton
- key_Label_Font: seting the button's Font'
- key_Handler: is a class that respond to keyCode such: 65...90 (A-Z)then do a [d]doClick()[/d],
- myAct: is a passed ActionListener from the calling class.
Many Thanks All.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: A JButton with Double Listener Posted: Apr 18, 2005 12:14 AM
Reply to this message Reply
What is the question?
Where do your problems lie?
More information is needed to give you an answer.

You posted allready the methods needed.
Now you just have to implement the listeners.


What is a TWO Listener? 2 listeners or a class wich implements both?

Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

Re: A JButton with Double Listener Posted: Apr 18, 2005 3:26 AM
Reply to this message Reply
What is the question?
R: The Question is How to make them work both Simultaneously using the mouse OR the keyboard.

Where do your problems lie?
R: The KeyListener do work when I use KeyCodes such as 65, 66 for e.g., but the ActionListener does not...

What is a TWO Listener?
R: KeyListener and ActionListener.

2 listeners or a class wich implements both?
R: One Method (ActionPerformed ) is implementing one, Another Class is implementing KeyListener (with keytyped, etc).
Many Thanks Matthias.

Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: A JButton with Double Listener Posted: Apr 18, 2005 4:38 AM
Reply to this message Reply
Hi,

I think you want to perform the mouse click action when a user presses a specific key. Or more clearly, ActionListener should be get called when user pressed a special key.

For this you can us setMnemonic(int) method. e.g.
button.setMnemonic(KeyEvent.VK_S);

When the user presses Alt+S the actionPerformed method of action listener gets called.

I am not in touch with Swing so please check whether it is Alt or Ctrl.

Regards,
Amol Brid.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: A JButton with Double Listener Posted: Apr 18, 2005 8:12 AM
Reply to this message Reply
How to make them work both Simultaneously
You don't need to do anything to do that.
Adding more than one listener automatically enables all listeners. Depending on what you do another one is called.
If 2 listeners need to do the same thing, then justcall the same method.

The mnemonic solution is probably what you need. If you need the button being activated with different keys, you need of course a listener for the key typed (or pressed) event, then call the same method as the action listener does or create a action performed event and fire it.

The action performed event can be activated in 3 ways: clicking on the button, using the mnemonic, typing "space" or "enter" while the button has the focus.


If you need instead a solution like "mouse klicked while holding Crtl button", you need to look at the MouseEvent and mask the event's value.

Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

Re: A JButton with Double Listener Posted: May 3, 2005 10:42 AM
Reply to this message Reply
Have been very busy these past days, doing my project, I'm taking time today to thanks all of you guy who did spare some of your time answering my question.
Many thanks for all of you,
:=).

Flat View: This topic has 5 replies on 1 page
Topic: JSwat VM does not work Previous Topic   Next Topic Topic: EJB compilation error - EJB does not exists

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use