actionPerformed() for button hold down - java

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

Related

Change JButton properties on mouse over

I am doing a calculator app with swing. I have changed the background color of JButton but when clicked its color changes to a blueish color. Can I set a color manually on mouse over and on click. like the windows 10 default calculator?
Try using this:
buttonName.setFocusPainted(false);
This will get rid of the default border when you click on a button.
To make the button do stuff ie change it's background you can implement the MouseListener interface for it, but I think the best thing for you would be to read about event handling in swing :)
Here's a link
You need to first detect mouse motion over the button using an Mouse listener .You can use an implemented version of it called the MouseAdapter
button.addMouseMotionListener(new MouseAdapter()//To
detect mouse motion
{
#override
public void mouseEntered(MouseEvent m)//called when mouse
enters first time into the button
{
button.setBackground(Color.blue);
}
#override
public void mouseClicked(MouseEvent m)//called when mouse is clicked[pressed and then released]
{
button.setBackground(//whatever you want);
}
});
Note for mouse clicks you can just change the color inside your action listener

ItemListener vs ChangeListener vs ActionListener

I've seen many Java Swing programs that use ActionListener, ChangeListener, or ItemListener. What are the differences between these and when should I use each one?
ActionListener
They are used with buttons or menu. So that whenever you click them it notifies the ActionEvent which in turn invokes the actionPreformed(ActionEvent e) function to perform the specified task.
ItemListeners
These are used with checkboxes, radio buttons, combo boxes kinds of stuff.
See what happens when you use ActionListener with combo box instead of item listener in this link https://coderanch.com/t/331788/java/add-listener-combo-drop-list.
ChangeListener
This is used with components like sliders, color choosers and spinners where you want the action to be performed according to the change in that component (https://docs.oracle.com/javase/tutorial/uiswing/events/changelistener.html). Focus on the word "change". Then you might think it should work with buttons too. You can see for yourself on this website http://www.java2s.com/Tutorial/Java/0240__Swing/AddchangelistenertoButtonmodel.htm
For a JMenuItem, instead of a listener, you should use an Action (which is a more capable form of ActionListener):
Action saveAction = new AbstractAction("Save") {
#Override
public void actionPerformed(ActionEvent event) {
saveDocument();
}
};
saveAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_S);
saveAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control S"));
saveMenuItem = new JMenuItem(saveAction);
For JCheckBoxMenuItems and JRadioButtonMenuItems, just as with regular JMenuItems, the Action’s actionPerformed method is called when the user activates the menu item. You can check the new state from within your Action:
Action showStatusAction = new AbstractAction("Show Status") {
#Override
public void actionPerformed(ActionEvent event) {
boolean selected = (Boolean) getValue(SELECTED_KEY);
statusBar.setVisible(selected);
}
};
showStatusAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_W);
showStatusAction.putValue(Action.SELECTED_KEY, true);
showStatusMenuItem = new JCheckBoxMenuItem(showStatusAction);
Note that Action.SELECTED_KEY only works if you set it to true or false before you install the Action. From the documentation:
Components that honor this property only use the value if it is non-null. For example, if you set an Action that has a null value for SELECTED_KEY on a JToggleButton, the JToggleButton will not update its selected state in any way. Similarly, any time the JToggleButton's selected state changes it will only set the value back on the Action if the Action has a non-null value for SELECTED_KEY.
If you insist on using listeners directly, ItemListener indicates selection state, so it can be used to monitor the state of JCheckBoxMenuItems and JRadioButtonMenuItems. For all other JMenuItems, use ActionListener.
The above actually applies to all descendants of AbstractButton as well as JMenuItem and its descendant classes:
For JButtons, use an Action.
For JToggleButtons, JCheckBoxes, and JRadioButtons, use an Action and check its SELECTED_KEY value.
If you aren’t willing to use Actions, use ActionListener for JButtons, and use ItemListener for JToggleButtons, JCheckBoxes, and JRadioButtons.
My understanding is that there is no reason to use a ChangeListener with a standard JMenuItem or button, as a ChangeEvent is mainly intended to indicate to renderers that the component needs to be repainted.

Vaadin ComboBox blur event gets triggered when enter pressed

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.

Change value when mouse is clicked

I have JTextfield. Now I want to change value when in this component is mouse clicked. For example: score (2 big JTextField) and when I click to one of these field it increase the value from 0:0 to 1:0.
Should I implement MouseListener or there is some easy way how I can do this? In mouse listener I need override just one method mouseClick and other method will be empty.
And another question: when should I implement MouseListener? e.getButton() return always 1 for left button and 3 for right button?
Should I implement MouseListener or there is some easy way how I can do this? In mouse listner I need override just one method mouseClick and other method will be empty.
Use a MouseAdapter.
An abstract adapter class for receiving mouse events. The methods in this class are empty. .. Extend this class to create a MouseEvent (including drag and motion events) or/and MouseWheelEvent listener and override the methods for the events of interest.
Now I want to change value when in this component is mouse clicked
JTextComponents are Focusable, look for FocusListener
Implementing MouseListener on your class is one way to do it, but if you just want to react to clicks, it's easier to use an anonymous class extending MouseAdapter
textField.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
// do your thing here
}
});
As for the second question, the API documentation quite nicely documents the return values of MouseEvent.getButton().

Swing make a JButton not focussable

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;
}
});

Categories