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

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.

Related

Adding timeout to a user interface program

I'm working on a Java project involving user interface, using the Button class and some action listeners.
I have a few buttons (each with an action listener) and I want to add a timeout to the whole program. That means, if no button was clicked-on in a certain amount of time, a specific action should be performed.
I tried adding it among the basic while loop + isDisposed() function. To my knowledge, this loop checks multiple times whether a button was clicked-on. For some reason, I couldn't get the outcome I wanted.
Is there a way to do so with the classes I mentioned? I also couldn't find any suitable functions in the Button class.
Use a Swing Timer (javax.swing.Timer).
Instanciate it with new Timer(CERTAIN_AMOUNT_OF_TIME, e -> timeoutAction()) (If you have a function timeoutAction), disable repeating with setRepeats(false) and start() it.
When the user clicks a button, call restart() on it.
Also, you’re mentioning the Button class, which is an AWT class. I recommend using Swing’s JButton class instead.
It is highly probable that you are using swing, but since this is not specified, I will give you a general answer, with links to swing examples.
First of all, since all your button clicks will behave in a very similar manner, so you will need a custom ActionListener (example). Your custom action listener should perform the action, but set a timestamp or some kind of date value to the current moment. In parallel, you should have a heartbeat event, which periodically (frequently) runs and compares the current moment with the timestamp set by the last button click. And you can see an example of a periodic task here: How to schedule a periodic task in Java?

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

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.

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.

Stop receiving all ActionEvents/Stop Listening in Java for a period of time?

I have a simple program that utilizes Java Swing Timer to display an image for 400 miliseconds, in this period of time I just want to stop all ActionListeners or stop taking ActionEvents. I've got 40+ buttons and want a simple way to do this.
Is there anyway to do that in Java?
Can you determine that you are in this "image displayed" state? The image goes up and you set the state to "image displayed" or whatever. Go through your widgets and decide which ones are supposed to be dead while the image is up. Turn them into Observers of this state value. When the state changes, they either enable or disable, as appropriate. The image code doesn't do anything directly to any widget. It just declares that the state is now "image displayed". It's up to the Observers to decide what to do, if anything, with that information.
Or use the GlassPane. That works too. Of course, the GlassPane shuts down everything. If you need to be more selective, you need a more fine-tuned approach.
You can use a temporary GlassPane instance to consume all events by registering empty listeners to it.
Use an undecorated modal JDialog to display the image. Before you make the dialog visible you would start a Timer. When the Timer fires in 400 ms you close the dialog.
I've had similar issues and typically found that its a design issue that got me in that situation. Being the case, I still had to find away around it. To fix the issue, I kept a list of the elements that I wanted to disable (stop listening) and iterated through them at the beginning and end of the timer. For buttons it should be as simple as:
for(Component c : listOfToggledComponents){
c.setEnabled(shouldItBeEnabled);
}
For buttons, this will grey out the button. Similar things happen to other swing components.

Categories