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);
Related
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.
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.
My Java application working flow:
get a input keyword, then translate it to a query to HTTP POST
new a Jframe, witch shows some words like "keyword processing... pls waiting."
do the POST in [1], it may take many minutes.
after POST returns a result, extract the data I need.
show the data in the Jframe, which replaces the content in [2]
I first try to code the flow logic in Jframe's constructor.
But the frame will hang on the HTTP POST stage, and doesn't show the words in [2].
After POST done, it shows the data in [5] directly.
Cause jumping the stage [2], users may think the process blocking means the something wrong, it does not do the search work.
Now I add a JOptionPane.showMessageDialog in stage[2]. It stops the process because needing a mouse click on "OK" Button in the dialog.
In that moment, the panels in the frame shows the words, "keyword processing... pls waiting.".
I'd like to know why the stop point of the MessageDialog can paint the old panel, which means the words in [2].
Such as revalidate() or repaint() do not paint the old panel.
Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See Concurrency in Swing for details and the fix (a SwingWorker).
BTW - Use a CardLayout as shown in this answer for changing panels.
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.
I'm using the java swing library to develop a board game called DAO.
The problem is that after the human player makes its move, by clicking on the JButton with the piece image that he wants to play, I call the computer AI routine but inside the mouse event function. By doing this only when the function returns, the computer ends its turn, do the JButtons refresh their Images (setIcon comes in).
I'd like to know how can I force the JButtons to change their image at the moment they are clicked and not only when the mouse event function ends (as I need to handle data inside it).
I've tried all of this
myButtons[i][j].setIcon(xIcon);
myButtons[i][j].revalidate();
myButtons[i][j].repaint();
myButtons[i][j].validate();
None worked.
Thx in advance
You may want to try putting the action performed upon clicking the JButton into a Swing worker. This will allow the task to go on in the background, while the user can still click other buttons, etc.
See http://java.sun.com/docs/books/tutorial/uiswing/concurrency/simple.html.
There is a single thread used for all Swing activity.
Here's the process.
One event appears on the event queue
it is pulled from the queue and executed by The AWT Thread
Any new events created while this is executing are placed on the queue to be held until the currently running AWT event returns.
The event executing returns and the next event on the queue is dequeued and executed.
This means that if you need to do anything that takes more than, say 1/100 of a second or so, you shouldn't do it any thread started from a swing event. Instead, spawn your own thread and return the swing thread to the system so the GUI can be updated.
Now, your thread MUST NOT update any GUI objects! If you need to update a GUI object, use invokeLater to place your code back on the AWT thread.
New Java programmers not conforming to this rule and executing tasks on the AWT thread is almost certainly the biggest reason people think Java is slow.