Make all the text box to non - editable upon a button click - java

I am new to Swing coding.I have 3 text box and one button.
Upon clicking of the button I want to disable/deactivate the three text boxes.
I added the below code in the button actionPerformed function.It doesn't disable/make it non editable.
myTextField.setEditable(false);
myTextField.setEnabled(false);
mytask() //it taskes almost 30 minutes
Can anyone please tell me what I am doing wrong?

mytask() //it taskes almost 30 minutes
Your long running task to should execute on a separate Thread. If you invoke the long running task in the ActionListener then the GUI will freeze until the task finishes executing.
Read the section from the Swing tutorial on Concurrency for more information. You will probably want to use a SwingWorker for the task.
I added the below code in the button actionPerformed function.It doesn't disable/make it non editable
If you already invoke the task in a separate Thread, then it means you don't have a reference to the text field that you add the frame. Maybe you created two instances.

Related

How to use JLabel and Swing Timer to briefly notify the user?

I'm working on a small app for a class. One of the functions I want is when a button is clicked, it adds an object to an ArrayList based on the fields that they filled. When they click they button, I want them to have visual confirmation that the action has been performed. However, just updating a JLabel would cause it to sit there forever after the button was clicked.
I would like the label to start off displaying "".
When the button is clicked, I want it to say "Character Added!"
..for a few seconds. and then switch back to "".
I looked up some questions about label updating using a timer, but most of them have been using it to display a counting down clock, and they usually use a timer start method. I'm confused about if you need a timer stop method to reset the label.
Any pointers?
What I ended up doing is, instead of using a JLabel that updates, I simply made it so when the button is pressed and a character is added to the ArrayList, I called
JOptionPane.showMessageDialogue("Character Added!");
That way instead of a small little JLabel confirming it, they get a very apparent notification that requires them to acknowledge it. Tons more simple than doing the whole Timer thing.

Java How to wait for task to finish before returning control

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.

Unable to modify or edit JFrame fields after clicking on a button

I have a JFrame with three text fields, two combo boxes and two Jbuttons. The coding is written in Java. One button is to start the execution of the automation script and another button is to abort the execution.
But after clicking on Start Execution button, I am unable to click on the second button and unable to edit other fields like text fields, combo boxes in the JFrame also.
As this is my project related I cannot post my code here. I apologise for that. I hope you can understand the logic or concept behind my problem. I have done a lot of search in internet but still no progress.
Please help me with this. I am using action listener behind the two Jbuttons.
The Event Dispaching Thread (EDT) should only and only do graphic related work. Any other work should be done in another thread (see SwingWorker).
Every event generated by swing, will run in the EDT, this includes actionPerformed()
That is happen because of the code implement you in the first button is continuously running , use java thread to do that stuff in the first button code. then that will be works fine.

not able to show data in a jTextField

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);

java help applet action listener

Can anyone point me in the right direct to this? I want the actionlistener in a applet to display 1 of 3 parameters in a jlabel retreived from the html file, When the appropriate option is selected from a combo box.
Cheers
For a combo box you want to add an ItemListener to your combo box to handle the change. You should create some sort of controller class (just a regular class which implements ItemListener) and add this as your listener. Then, in the itemStateChanged method, you program your logic. You probably want to give the ItemListener you create a reference to your panel so that it can make the appropriate changes.
One other note. If you're reading the HTML file on the fly when the drop down is selected, the user interface will freeze up until this operation is complete. This is because these events are fired and executed on the Event Dispatch Thread, the main thread for user interaction. To avoid this, you can either parse the HTML file ahead of time into memory or you can spawn a separate thread in the itemStateChanged event to parse the HTML file. Just remember, when you're done, to use SwingUtilities.invokeLater to get another runnable back on the Event Dispatch Thread to update the drop down. All updates to the Swing user interface must be done from within the Event Dispatch Thread.

Categories