Hi gurus's :cool:

I have got an actionListner in my code. The problem is that when I try to set my JLabel as true it does not work and does not show up in the GUI ? :@

 new ActionListner(){
   public void actionPerformed(ActionEvent e){

       myFunction() //here's some timeconsuming Function called.


Now if I add a second actionListener to the same Button the Label works but requires 2 Mouse Presses which is bad :(

Why is it happening so and how can I get it work the way I want ? :-/

Thanks :confused:

Don't have time to explain now, but your setVisible on line 5 won't do anything until after your actionPerformed method terminates. It's a threading problem - Google the Swing Event Dispatch Thread.

I did not get your question.
but when we click any button we check its ActionEvent.getActionCommand().This function returns string on which button we have clicked.And based on that we make action i.e. write a code.
For ex. I have button with text "Add" on it ,then in my
function 'actionPerformed' I will write as

public void actionPerformed(ActionEvent ae)
       if(ae.getActionCommand() == "Add")


                         myFunction() //here's some timeconsuming Function called.


I think this may help you.
If you have doubt let me know.


Change your code to start a new thread to do the long running task on its own thread, not on Swing's GUI thread. When that task ends, have it do the setVisible(false)

I believe his question is that the label does not SHOW when the button is pressed, is that right? If what I am saying is the question, then post the entire code, because sometimes it could be a simple mistake like forgetting to ADD the Jlabel to the contentPane of the JFrame, forgetting to set the size and boundaries, especially if the layout manager is specifically made null, the JFrame will not display the Label unless boundaries are set.

I'll say it one mire time. The code as posted will NEVER display the label because it's made invisible again before the action method returns. Swing is single-threaded. Repaints and action methods execute on the same thread. The repaint will not happen until the action method terminates, at which time the label's visibility is false.
NormR1 is right.(Use a SwingWorker thread). The other posts are irrelevant.

Oh yes, Never mind James is correct, you have to remove the setVisible(false); from the code, but even after that, just make sure that you initialize the JLabel properly and have a proper LayoutManager :D good luck!

Hi James is there any other way to make it visible ? Can you give me some example

Leave the setVisible(true) as it is.
Run myFunction in a SwingWorker thread (see the API or online tutorials)
call the setVisible(false) immediatley after myFunction runs (eg add it to t he end of myFunction - simple, even if it's bad design).
This is the way - a bit of a learning curve, but essential for real JAva programming anyway.

Is your JLabel's instance on a background painted by the Graphics in AWT?
If the background is made by the Graphics's object, there will be a trouble. The stuff in AWT is considered as heavyweighted while the stuff, such as JLabel's instance in swing, lightweighted. The heaveweighted stuff always "overwrites" the lightweighted one.
Use Label in AWT rather than JLabe in swing.

Thanks James. I am able to execute my long process in swingworker thread. Now the other problem is that I am able to run my function only once. It does not execute when i try to run it again using my JButton.

I just read that "SwingWorker is only designed to be executed once. Executing a SwingWorker more than once will not result in invoking the doInBackground method twice. "

In this case how can I run my code which uses swingworker multiple times like suppose using my JButton ? Is there any way ?

Thanks for all your help

You need to create a new instance of your SwingWorker class each time the button is pressed - I don't have your code, but presumably you create the instance once, then start it in the button's action handler - in which case just move the code that creates the new instance into the handler.

Thanks a lot James :). It works ;). IT was a great learning experience for me since I had never used SwingWorker before :icon_evil:.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.