How to exit from Java Swing application, avoiding messages generated from setInputVerifier - java

I have a text field where I have imposed some constraint in input data, by the help of InputVerifier.
The constraint is if the text field is left empty, then an error message will pop up. When the application starts, the cursor remains on the text field (text_field.requestFocusInWindow()).
Now, If the exit button is clicked, at anytime, The application should terminate immediately. But if the text field remains empty, it displays the error message and doesn't exit. How to solve this problem?
Exit Code:
dispose();
System.exit(0);

Ok, let's say your textfield is in a JFrame. Then register a WindowEventListener on the frame. Catch the event that closes the window and remove the InputVerifiers from all textfields. Then call System.exit().

Related

actionPerformed has empty logic

I have a screen which has list. By right click, I can open a small pop up and add new records to the that list by choosing some record and clicking OK button from pop up.
OK button which is on pop up has an action listener like below:
okButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
}
});
And that is all the actionPerformed method does and I do not understand how this method is adding new record to the list on main window. There should be another part of code which is connected to this part but I do not know what is that. Do you have any idea what I do not see on that logic?
The ActionListener is not empty and in fact it is changing the state of the window that holds the JButton, making it no longer visible.
No one can say with 100% confidence what logic is being used here since you've yet to show enough code for that, but our guess is that this button is being held within a modal JDialog -- a window that freezes code flow in the calling code once the dialog window is visible, and (here's the key) that releases the block on code flow once this dialog is no longer visible. So in this situation, making the dialog no longer visible will allow the calling code, the code that initially told the dialog to display itself, to resume flow of its logic. Presumably in the subsequent code, it will query the dialog for data that was entered, and extract it, again the details of which are in code not yet shown to us.

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,

Unable to focus JOptionPane on foreground with default frame

I have a piece of code in my program where in I need to display an error message. Code:
String ErrorMsg=" Error to be Diplayed ";
JOptionPane.showMessageDialog(null, ErrorMsg, "Failure", JOptionPane.ERROR_MESSAGE);
Note: Default frame is used.
The message is successfully displayed, but before acknowledging by pressing "OK" button if I try any other successful flow the message box control is lost and message box won't be on foreground blocking even the successive flows.
I want the Message Box to be on the foreground always until the user presses "OK" button, rather losing focus and getting hidden. How to do that?
If you want JOptionPane to behave as it would in a full-fledged GUI, then first create a full-fledged Swing GUI. Forget using "default" frames or whatever you're using (the console perhaps). You are desiring GUI behavior, and so to get this you must create a GUI by displaying your application in a JFrame and have the JFrame launch the JOptionPane.
Pass reference to the parent frame instead of the null (first param).
I want the Message Box to be on the foreground always until the user presses "OK" button, rather losing focus and getting hidden.
Use a JFrame and setAlwaysOnTop(true). You will need to display your own message and button.
A JOptionPane uses a JDialog behind the scenes. A JDialog does not support this property.
Edit:
To get the icon used by the option pane you can use:
Icon icon = UIManager.getIcon("OptionPane.errorIcon");
For a list of the other icons see: UIManager Defaults.
The below code is enough to make a JoptionPane message with default frame to be set on top .
JDialog dialog = new JOptionPane("ErrorMsg",JOptionPane.ERROR_MESSAGE,JOptionPane.DEFAULT_OPTION).createDialog(" Failure");
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
dialog.dispose();

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.

JFrame continuing to receive key strokes even after seeming to lose focus

I have a Java app that I'll call App. App will occasionally display a JFrame that we'll call myFrame. App will also display a JTextArea that is contained in either a JDialog or a JFrame (I'm not sure which, but I can find out if that's necessary to answer this question). Let's call this JTextArea "myTextArea".
Now, the following sequence of events happens:
We display myFrame. It has the focus and you can give it input.
We call myFrame.setVisible(false)
We display myTextArea.
We call myTextArea.requestFocus().
myTextArea has the focus (the cursor is blinking with in it), but all the keystrokes that are input are sent to myFrame!
Note that myTextArea is not contained in myFrame.
What is going on here? Has anyone heard of a non-visible JFrame receiving keystrokes? Not only receiving keystrokes but stealing them from some other component that has the focus?
I found what’s basically causing the problem. MyFrame has a class MyKeyEventHandler that implements KeyEventDispatcher. The method dispatchKeyEvent(KeyEvent e) is always returning false even for key strokes that are intended for myTextArea. Therefore the key strokes do not reach myTextArea.
It is not about toggling the visibility. The JFrame is initialized first and still has focus. You are only making it invisible, not taking away the focus from it.
Moreover, your JTextBox needs to have a parent container. Possibly
myFrame.add(myTextArea);
should work. To shift the focus to the JTextArea, use :
myTextArea.requestFocus();

Categories

Resources