I have a continous Action which I can call by pressing down a JButton b1. Currently I have added a MouseListener to b1 and I control the continous nature of this action by calling StartAction() in MousePressed() and StopAction() in MouseReleased(), if the source of the mouse event happens to be b1.
Now I want to be able to trigger the button by keyboard as well. One would usually do that by ActionListeners. I can do:
if (source.equals(b1)) {
startAction();
stopAction();
}
Only problem is, this way a mouse click would trigger both the ActionListener and the mouseListener. I thought I would try setting the action listener as:
if(source.equals(b1) && !(e instanceof MouseEvent))
{
...
}
But that gives me: inconvertible types cannot cast MouseEvent to ActionEvent.
So Question is: is there any way of knowing if an action event was a mouse click or an Keyboard trigger? Should I use another type of Buttons?
Related
I need to add a Enter key shortcut listener to a ComboBox. I only need the shortcut to work when the ComboBox is focused. I used the approach described in this answer.
combo.addFocusListener(new FocusListener() {
#Override
public void focus(FocusEvent event) {
combo.addShortcutListener(shortcutListener);
}
});
combo.addBlurListener(new BlurListener() {
#Override
public void blur(BlurEvent event) {
combo.removeShortcutListener(shortcutListener);
}
});
What it does is, adding the shortcut listener when the combo box got focus and removing the shortcut listener when combo box lost focus.
This works well for TextFields but does not work for ComboBox. The reason is, whenever I press enter on the ComboBox, blur event gets called instead of shortcut listener getting called. Since shortcut listener is removed when blur event gets fired, shortcut listener never gets fired.
Why does combo box trigger a blur event when enter is pressed? How can I get this fixed?
As discussed above in comments, wrap the the combobox is panel and add shortcut listener to it. This should work.
Why? When you add a shortcut listener by default the scope of this shortcut listener is added to enclosing Panel/Window/UI(basically single component containers).
Hope this helps.
I know what interface is and how to build/use one. Lets take the ActionListener interface for example.
My question is what happens after I click on a button, which class calls the actionPerformed method?what is the process from the part that I click the button to the part that the actionPerformed is executed?
The JButton calls the ActionListener.
Internally, it listens to keyboard and mouse events. When it receives a mouse click or a keypress which means "click the button", it creates an ActionEvent instance, loops through all the ActionListener instances that have been added to itself, and calls each one with the ActionEvent as argument.
Right now I have:
panel.getZoomButton().addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ACtionEvent e)
{
zoom();
}
}
This is called every time the zoom button is pressed. How can I change it so that zoom() will be continuously called if the zoom button is being held down?
You will need to use a MouseListener and override the mousePressed() method. There you can use a Timer or something similar to measure the time, the button has been pressed, in order to calculate your zoom.
Perhaps this question helps you with that: Java MouseEvent, check if pressed down
use Swing Action (most scallable abstraction) instead of ActionListener, there you can to set isEnabled, to switch to false value until all events are done
or add Swing Timer for reset Boolean to true value
there is possible to setMultiClickThreshhold(long threshhold), but is aplicable only for MouseEvents, this methods doesn't react to KeyBindings (ENTER and TAB) firing from Keyboard
is it possible to convert a MouseEvent to an ActionEvent?
Not without losing some information. The MouseEvent contains information about the mouse location (x, y) and which buttons that are pressed (if any).
I would do the conversion like this:
MouseEvent me = ...;
ActionEvent ae = new ActionEvent(me.getSource(), me.getID(), me.paramString());
Sure, that's what a Button does (to my understanding). It processes a MouseEvent and creates (sends) an ActionEvent.
Action events are semantic events - like a signal, that a certain button (widget!) has been "pressed". The trigger for this action event may have been a mouse event ("left button has been pressed and released while the mouse pointer was in the rectangle defined by a AWT Button widget") or a keyboard event ("Space bar has been pressed and released while the focus was at AWT Button widget").
I guess you're not looking at a technical conversion. Practiacally, you'll have to listen to mouse events and fire new action events to your action listeners.
I want to make a Java swing button 'not-focussable'.
The button should not receive focus at all, but should be able to receive mouse clicks.
I thought the following options, but these either dont solve my problem completely, or dont seem elegant. Are there any other/better/suggested options?
Move the focus to the next component immediately when the button receives focus (but then what do I do if the button is the only component on the UI other than labels?)
Implement another non-focusable component as a button (a label with mouse events, borders...) (this does not look very elegant to me)
Create a anonymous button implementation that overides the keyboard events so that it does not respond to keyboard events (this does not solve the focus problem, but is somwhat ok for me, since the root of the problem is to avoid accidental keyboard clicks. I will do this only if there are no options at all, but even then prefer option 2)
All Swing components have a setFocusable method to do this:
JButton button = ...
button.setFocusable(false);
Did you try to call the setFocusable() method inherited from java.awt.Component ?
Resources :
Javadoc - Component.isFocusable()
Oracle.com - Focus tutorial
You can implement your own FocusTraversalPolicy (or extend e.g. ContainerOrderFocusTraversalPolicy) with an accept method that just doesn't like your button.
JFrame frame = new JFrame();
... /* create other components */
frame.setFocusTraversalPolicy(new ContainerOrderFocusTraversalPolicy() {
public boolean accept(Component c) {
return super.accept(c) && c!=iDontLikeYouButton;
}
});