Swing Events Trouble - java

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.

Related

ActionListener running before focus listener

I have an application in which there is a textbox and a button. the textbox has focusListener(for incorrect values) and the button has actionListener(for saving the value into a file).
An error message window pops up when a wrong value is entered in the textfield as soon as it loses focus. Now I have 2 senarios.
when the focus is lost from tab out and if a wrong value is entered in the field then the pop up window shows up correctly.
But when i enter a wrong value in the field and without tab out I click the button then the actionListener is activated before the focusListener(Focus Lost) and it saves the wrong value in the text file and then shows the error message.
How should I stop the incorrect value to be saved into file by running the focuslistener first?
ActionListener running before focus listener
Focus, FocusListener, Focus Subsystem is asyncronous, you can sheduling those events, but can caused another side effects, I'd suggest don't do that
delay required events in EDT by using invokeLater,

How to implement cancel button in JConfirmDialog

I have a java swing application. This application contains a mainframe window.
When the user clicks the close (X button on top right of window), my application pops up a JOPtionPane Confirm dialog with yes, no and cancel operations. Clicking on yes saves some files and closes the application, No closes the application without saving the results. This all has been implemented and working fine.
Now i need to implement cancel operation which should typically do something like close the ConfirmDialog and keep the application still open (contrary to this yes and no option closes the application)". I need to implement the idea of "application should not be closed upon clicking the cancel button". For any existing example one can consider the closing of excel sheet (after you edit the excel and try closing without saving).
Set the default close operation for the frame to do nothing:
frame.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE )
Your windowClosing event handler can then literally just return if the user clicks cancel and the program will continue as if nothing has happened.
If the user clicks yes or no then your code will need to programmatically close the frame.

How do I direct key board input to non-focused JTextField?

Some background:
I have a turn based game where you can play several concurrent games in separate windows. Each window/game has it's own chat and also a kind of game action panel that turns up when it's your turn to act.
The issue:
Game windows will steal focus whenever it becomes your turn to act in any of the games you're playing, this is by design but very annoying if you're involved in chatting at any of your games because when focus is lost that chat will no longer receive your key board strokes.
What I want:
Some way to dispatch key board events to a JTextField that's no longer the focus owner (and also in a different window/JFrame than the current focus owner). Is there some way to do this? And how?
Plan b would be to set some kind of timer on the chat and let the window refuse to give up focus until x amount of time has passed since the last key stroke in the chat, but it might not be ok to stall the focus switch since your action time is limited already.
You can set alwaysOnTop of the current chat window true while there's text in the chat text field by calling:
java.awt.Window
public final void setAlwaysOnTop(boolean alwaysOnTop)
throws SecurityException
When user presses Enter or clicks on Send button reset the alwaysOnTop so other windows could steel the focus.
I might have found what I was looking for here. Remains to be seen if I'll try to use it or if we'll change the requirements :)
See this link to find a working SSCCE on how to redispatch KeyEvents to any text component.

JButton action Listener

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.

regain focus after java.awt.Robot.keyPress()/mousePress()

I writing an application which controls another application by using the keyboard only. To more concrete, the application simulates key presses and mouse clicks when a certain key is pressed on the keyboard. For example, pressing on the 'x' key simulates a mouse click on the [X] in the rop right corner, followed by a little sleep of 2 seconds and an 'enter' to confirm the exit dialog. Pretty easy. I am developing this application in Java.
Sending a key press or a mouse click is very easy with java.awt.Robot. I am facing one little problem. Say I have configured a key which will click somewhere on the screen. The problem is that consecutive key presses aren't catched anymore, as my application lost its focus caused by the mouse click outside it's window.
My question now is: what is the best way to be sure that my main application keeps the focus? Is there a way to focus my application again after the key presses and mouse clicks are sent out? Is there a better way?
Thanks in advance.
If your application lost the focus. because you or your Robot clicked to somwhere else, the Robot must click on the application again before sending a new key. In c/c++ you could force the focus to the application (a non-trivial task), not in Java!
You might want to take a look at Component.requestFocus() to see if can do what you want.
Be aware however that window focusing has very platform dependent behaviour, so you will probably need to do quite a bit of testing to ensure that your code does what you want in all circumstances.
I managed a way to prevent applications from losing all focus in Java.
By placing a WindowFocusListener on the frame (or dialog) and calling setVisible(false) followed by setVisible(true) in windowLostFocus the component will re-appear as soon as it is dissapears (not the prettiest solution but it does work).
By then calling component.requestFocus() your robot should be able to continue where it left off

Categories

Resources