JButton action Listener - java

Hey there, I'm searching for a Key Listener which always activates himself when the mouse is pressed down... for example if I keep pressing the button it will always write something on the console and stops writing that when I releas the mouse button.

Firstly, KeyListeners respond to keyboard events: MouseListeners respond to mouse events. However if you are dealing with a JButton you probably want an ActionListener attached to the button. That will respond when the JButton is pressed, which can be in different ways, not involving the mouse.
Listeners don't generally do that sort of thing. What you need is a Timer that will write something to the console repeatedly at some interval. Then you use a ActionListener to start the process going when the button is pressed, and stop it when the button is released. The documentation for those classes should give you what you need.

You could do that with a MouseListener which signals the app to start printing something on MousePressed, signals to stop printing on MouseReleased.
You should do the printing in a separate thread as to prevent the Swing GUI from freezing.

Related

Add actionlistener to jpanel

I would like to know if there is any way to add an ActionListener to a JPanel?
I have no problem adding those to JButtons, but JPanel seems not to have such a method.
Basically I have a bunch of JPanels inside a JFrame with a grid layout and I would like to know if there is any way to know when the user has clicked on one of them.
Any help much appreciated!
An action listener serves to listen to action events. A button triggers an action event when it's pressed and released using the mouse (and not when it's clicked), or when it's pressed using the keyboard (space bar, mnemonic, keyboard shortcut, etc.). It's a high-level event. A mouse click is a more low-level event, which is handled by a MouseListener.
Add a MouseListener to your JPanel if you want to handle mouse clicks.
Aparently you can use AddMouseLitener..
Silly me..

JTextField change value automatically while button pressed

I have a jTextField with two JButtons (Up arrow and Down arrow buttons). Clicking the Up arrow button the numeric value in the textfield increases by 1 (++), and clicking the Down arrow button the numeric value in the textfield decreases by 1 (--).
What I want know is how to automatically "scroll/change" the value while the button is pressed?
Thank you
What you probably want is a JSpinner. More specifically a SpinnerNumberModel.
Here is a link to a demo
http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html
The JSpinner is the best way to do this.
But if you want a different implementation, I would suggest using a MouseListener attached to the JButtons. When one of the button is pressed (the mousePressed event), a javax.swing.Timer is started. Every x milliseconds (depending on how fast you want the number increased/decreased) a check is made to see if the JButton is still pressed and if the mouse is still over the JButton. If it is, the number is increased/decreased. When the user releases the mouse (the mouseReleased event), the Timer is stopped/cancelled.
I never did this, so I don't know for sure that it works. But this is the way I would try it.

Swing Events Trouble

I am new to Java and am developing a java swing application.
The main frame (JFrame) has a text box and an OK button. There is some long processing to be done when the focus from the text box is lost as well as different long processing when the OK button is clicked. Now if the user enters a value in the text box and clicks the OK button directly, ideally, first the focus lost event is fired and then the event on the OK button. The problem is that while the focus lost event is running a joption frame comes up asking the user for some input, but even before user enters the input here, the OK button event starts executing leading to problems in the application. How can I serialize the event calls.
Any help will be appreciated.
Your problem lies within the concept of the Event Dispatch Thread. For long running work loads, check out the SwingWorker class.

My KeyEvents don't get registered while the Timer is firing events. How to fix this?

java noob here. In my Application class, I have a JPanel with focusable set to true and a KeyListener class added to it. I also have a Timer registered with a TimerListener class set up in the Application's main function. The KeyEvents work, but once I press the JButton to call timer.start(), KeyEvents stop firing and only the timer's actionPerformed executes. When timer.stop() gets called, the KeyEvents still don't work. I know I've set this up wrong, but I can't figure out how to fix it. Can anyone help?
but once I press the JButton to call
timer.start(), KeyEvents stop firing
and only the timer's actionPerformed
executes.
KeyEvents are only passed to the component with focus.
If you click on the button, then it has focus, not the panel.
Try resetting focus back on the panel in the button ActionListener code.
Or a better solution is to use Key Bindings. You can set up the key bindings so that the Action will still be invoked when the focus is on the panel or another component on the panel.

How are multi-button presses handled in swing?

I'm experiencing some strange behaviour when using a stylus with swing.
I am interpreting pressing the button on the side of the stylus(RIGHT) and pressing the stylus down(LEFT) as a "Grab" event, but occasionally (more often than 0), events are just being dropped.
The JavaDocs for MouseEvent are pretty explicit about how multibutton presses are handled if executed one at a time (left down, right down, right up, left up) but say nothing about simultaneous button presses.
I'm left to wonder, would they be emitted as two mousePressed events, or as one with the button mask set for both buttons, or something else entirely?
Thanks.
I'd interpret the API doc as simultaneous button presses being simply not possible:
When multiple mouse buttons are pressed, each press, release, and click results in a separate event.
So there should be separate events. The problems you observe could be due to errors in your code, the stylus' driver, the hardware, or Swing (this is in decreasing order of likelihood as I see it :)
I'd try to diagnose the problem by logging events at different levels, if possible.
Simultaneous button presses are processed as two separate mousePressed events. Run the Mouse Events Demo to see them processed separately.
As I recall, there's no way to handle simultaneous button presses. What I used to do to ensure that multiple buttons being pressed at once were treated as such was I would have a boolean variable for each button and when it was pressed, I would set it to true and when it was released, I would set the boolean to false. Then when it came time to perform an action, I would check for the boolean variables (sometimes I would have the actionlistener redirect to the method call for determining what action was to happen next after setting the booleans). This doesn't work if the only thing you want to do is them being pressed at the exact same time, but if you're just trying to get combinations to work, then that's how I did it. This was about 4 years ago, before Java 5, so I may be wrong about that.

Categories

Resources