I'm wanting to get rid of the text that is already set in my JTextField when I click inside the JTextField (focus on the JTextField).
Here's my code:
if (newSerial.isFocusOwner())
{
newSerial.setText("");
}
How do I get this code to run whilst I still have my JOptionPane.showMessageDialog running?
I'm not sure if I understand exactly what you are trying to do, but based on my interpretation of your question, you have a text field that you want to update while a JOptionPage message dialog is displayed.
JOptionPane dialogs block the Swing Event Dispatch Thread (EDT), so you can't update the UI while they are being displayed, unless you do it from a different thread (but that's not great - Swing components should be updated on the EDT). If I read your question correctly, the easiest option might be to replace your JOptionPane.showMessageDialog(...) can with a JDialog that isn't a modal dialog, and therefore, won't block the EDT.
Related
Is there a possibility to prevent a JOptionPane dialog from blocking the interaction with the rest of the program, especially child JFrames? In my GUI, I launch a JFrame and want a message dialog to pop up after the child is closed to remind the user of something, but they launch parallel and the reminder blocks the child frame from being used.
Like here:
popupObjMan newPopup1 = new popupObjMan(gatewayAbstract, gatewayAbstractID);
JOptionPane.showInternalMessageDialog(this, "REMINDER: DO REFRESH");
I've tried to set the popup always on top, but this doesn't quite do the job.
I have no problem with them launching parallel (I'd even prefer it), but I could not work my head around it yet.
I just started Java programming ,so sorry in case that'd be something obvious.
A JOptionPane normally need to be modal. It shows something important and waits till the user answers with whatever option you give him (e.g. ok-button, yes/no-buttons, ...)
But there are several ways to reach your target.
(a)
Normally a JOptionPane creates a modal window.
You need a modeless window which does not block other windows.
https://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html
(b)
You can start different threads to work for your different windows. They can have windows which are shown whenever the responsible thread commands them to. This is a bit difficult and can lead to memory-troubles.
(c)
You can write your own message-panels (e.g. notificaton) which are shown when and how long you like.
Bigger projects use different of these ways to achieve their goals.
A JOptionPane is a component, just like a JPanel. As a component it can be added to any other panel.
The JOptionPane API provides static methods to create a show the JOptionPane on a modal JDialog by default. You can't change this behaviour.
However, you can manually add the JOptionPane to a non-modal JDialog that you create. This is extra work as you now need to handle the closing of the dialog and processing the clicked button.
If you really want to do this then read the JOptionPane API. There is a section on Direct Use which demonstrates the basic code needed to add the JOptionPane to a JDialog.
This is a general question because i don't know what to search. But i have the following.
JPanel, put in some user information and press FINISH button, this goes to a JChooser to save the file. After they press save the program goes back to the JPanel and then closes. If there is a lot of user data, the JPanel will come back before the write is finished!
After they give a filename and press save, i want a progress/load bar to indicate the status of the save. How do i do this, how do it wait to finish the save before going back to the JPanel?
What is this called?
You should do several things:
First and foremost, do all the file writing and reading in a background thread so as not to freeze the Swing event thread. A SwingWorker would work well for this.
If you need to display the progress of a long-running process being run in the SwingWorker, then update its progress property within its doInBackground() method as the process runs.
Then add a PropertyChangeListener to the SwingWorker and listen for changes to this "bound" property. The property's name is "progress", so this should be easy.
Then in the above listener, update the value of your JProgressBar.
Next display the progress of the JProgressBar in a modal JDialog or JOptionPane (which is a variant of a modal JDialog). This will prevent the parent window from getting focus or running code until the dialog is no longer visible.
I have action thread and since it is Swing software, EDT.
I want my program to draw dialog window, and when it appears and it's filled with data, I want to get focus on selected text field.
Code flow: When I execute, it will run main thread, which calls method to draw dialog in invokeLater on EDT. Then program proceeds and in main thread it calls next methods that are being run in ED thread, again using invokeLater.
Problem: When I run it normally, it will not get focus on my text field.
Observation: But when I add some sleep (300 milis) to main thread, introducing time gap between one invokeLater call and next call in EDT, it works just like I want.
It seems to me like two actions added to AWT queue must be separated by some time, otherwise the second one doesn't work. I mean here setVisible(true) on dialog, and then requestFocus() on textField. Maybe requestFocus() only work when it sees dialog window drawn?
Question:How can I make things work, some synchronization method, maybe checking on dialog before calling requestFocus() (may be hard, because its in other class).
Solution:I forgot about most important thing - after calling setVisible() next thing I do is call to setEnabled(false) so user cannot do anything before data filling is completed. The problem was there, in setEnabled() I also was adding tasks to AWT queue (by invokeLater()). This task caused corruption of next steps. What I do now to fix it is calling this setEnabled(false) from my main thread inside invokeAndWait(). If I understand it correctly, now the dialog popup section is called first, and then main thread waits until EDT proceed his work and then setEnabled(false) is called. So technically user is not enabled to do anything after the window is drawn, which makes sense for me.
Anyway thanks for your responses.
It's better to call the focus setting from the dialog. Add a WindowListener to the dialog and use either
public void windowOpened(WindowEvent e)
public void windowActivated(WindowEvent e);
to set focus on the JTextField instance
The requestFocusInWindow() method can only be invoked on a visible component. That means the frame/dialog must already be visible when you invoke the method.
If you are trying to do this on a modal dialog you may have problems. Check out Dialog Focus for a simple listener you can use to set focus on a component.
I am writing a socket programming. It has GUI for server and client. In the server GUI there is a textfield which shows the word requested by user.
But I am having problem in showing the word.
I have tried
txtWord.setText(sentword);
It is not showing the word in the textfield. But when I write this
txtWord.setText(sentword);
JOptionPane.showMessageDialog(null, "the requesed word is: "+sentword);
then it shows the word in textfield and also shows it in the messagebox.
I have tried repaint() but it dint work.
Please suggest me some solution as soon as possible
as #Binyamin Sharet correctly commented, you have a Concurrency in Swing issue.
your Swing GUI doesn't care about long and hard tasks you're running in the background
even JTextField#setText() is declared as thread safe, output from Socket (i.e.) by default never notified Event Dispatch Thread
correct way could be to use a SwingWorker that has been created specifically to run long and hard tasks background to the Swing GUI and output to the GUI on event thread or EDT
or even easier is to use a Runnable in a Thread but making sure that all output to the Swing GUI is queued on the Swing event thread by placing it in a Runnable and calling it with invokeLater()
A dirty hack is to wrap code lines like so:
txtWord.setText(sentword);
JOptionPane.showMessageDialog(null, "the requesed word is: "+sentword);
into invokeLater(), but in this case your GUI will be unresponsive to Mouse or Keyboard events until Socket (in your case) ended
txtWord.requestFocus();
textField does not show up until the window is over the textField and back or it gains focus, until Clicking on it. So... just request focus.
Also if check the text size if you had set while creation.Sometimes text not displayed if there is mismatch in size
eg: txtWord.setSize(200, 24);
I'm having a problem..
In my code I have it so that text should output to a JTextField. when I run the program, it doesn't. However, if I directy after my code for putting text into the JTextField put a JOptionPane then it works...
Anyone have an solution to make the JTextField update without having the JOptionPane after?
My code:
// Works:
JTextField.setText("String");
JOptionPane.showMessageDialog(null, "String");
// Doesn't Work:
JTextField.setText("String");
//JOptionPane.showMessageDialog(null, "String");
There are two reasons why this might fail:
You're calling setText() from outside the main (Swing) thread
You're calling setText() from in the main (Swing) thread
In the first case, wrap the call in SwingUtilities.invokeLater().
In the latter case, you set the text but you're blocking the Swing thread, so the change can't be rendered. You will need to create a background worker to do the work and use SwingUtilities.invokeLater() to update the text field from your worker thread.
[EDIT] See the Swing tutorial for an example how to use background thread and how to update the UI from there: http://download.oracle.com/javase/tutorial/uiswing/concurrency/interim.html
Why not just use repaint() after the text update?
If memory serves me right that should address the issue of the text not appending.