Unable to change button text to icon immediate after click button in java swing -


I have written the following code. I have shown the full source but not the psudo code.

  The class UI has made JFrame {// with ButtonClick () a button) // // button before the // set icon, open the file, open the file and press the file button Get the .set text (""); Image Icon Progress Bar = New Imagine (Dataset Explorer UI Clash. GETRSource ("/ progreassbar.gif")); buttonExport.setIcon (progress); // Some databases were operated // Then removed the button from button button. Set icon (empty); button.setText ("click on"); }}   

When I click on the button, this file opens an open dialog and the button is set to clear the text but it does not set the icon on the button. When all the database operations are performed, the icon is displayed on the button that is set after the mark. Why is this behavior? How to set the icon on the button and perform some database operations and then delete it? Thank you.

The GUI system can only do the same thing at a time, like most codes Except that it uses threads) Calling your listener is one thing, the GUI system can not do anything because of your listener.

Your database operation must be run on another thread (which you can create) and then update the GUI. Something like this:

  There is a button here to open the ButtonPressed () file file dialog on the zero .set text (""); Image Icon Progress Bar = New Imagine (Dataset Explorer UI Clash. GETRSource ("/ progreassbar.gif")); buttonExport.setIcon (progress); New thread () {@Override public void run () {// Some database operations do something here EventQueue.invokeLater (New Runnabel) {@Override public void run () {// Then remove the icon from the button button .set icon (empty); Button .set text ("click");}}); } }.Start(); }   

The code runs in the same thread at the same time. It's convenient but dangerous Be careful while accessing data from new threads - If a thread changes a field and the second thread reads it, the result might be what you do not expect. The easiest thing to do is to ensure that the main thread does not turn on any variable used by a new thread.

When your database operation is finished, you can not set the button back, just by making a setText call, the main thread is allowed to affect the GUI only - what if the main thread screen Was drawing the button at the same time changing the database operation thread text? The button can be prepared incorrectly. Therefore, you need to call the EventQueue.invokeLater which asks to run your code in the near future, when it is not busy. The new Runnabel () {} button inside the code is like the code in the listener - no other GUI-related code will run until it will happen.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -