aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Different behavior after mouse click. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Different behavior after mouse click." Watch "Different behavior after mouse click." New topic
Author

Different behavior after mouse click.

Gene Falk
Greenhorn

Joined: Apr 30, 2014
Posts: 4
When this code executes in line, it correctly displays the first picture for 2 seconds, then the second picture.

When this code executes as the result of a mouse click, it displays no picture for four seconds, then displays only the second picture???


public void actionPerformed (ActionEvent referenceToTheActionEventClass )
{
JFrame picFrame = new JFrame ( "Pictures" );

ImageIcon myImage1 = new ImageIcon("picture1.png");
ImageIcon myImage2 = new ImageIcon("picture2.png");

FlowLayout myFlow = new FlowLayout();

JLabel picLabel1 = new JLabel(myImage1);
JLabel picLabel2 = new JLabel(myImage2);

Long time = 2000L;
picFrame.setSize ( 200, 400 );
picFrame.setLayout ( myFlow );
picFrame.setVisible (true);

picFrame.add (picLabel1);
picLabel1.setVisible (true);
try { Thread.sleep( time ); }
catch ( InterruptedException oops){ }
picLabel1.setVisible (false);

picFrame.add (picLabel2);
try { Thread.sleep( time ); }
catch ( InterruptedException oops){ }
}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Don't use a Long when you need a long.
Why are you adding components in that listener? You don't want to do that. It gives a very peculiar effect if components are added after the GUI has been displayed. I suggest you consider card layout which allows components to disappear to the back or appear towards the front.
You should consider a Timer instead of Thread.sleep.
Don't write empty catch blocks, otherwise you would never know whether an Exception has occurred.
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 96
The reason you don't see anything for four seconds and then see the second image is that the thread that's responsible for drawing those images is the same thread that handles events: the Event Dispatch Thread (EDT).

What happens is that when the ActionEvent is triggered, the EDT adds the label with the first image, sleeps for two seconds, adds the second image, and sleeps for two more seconds. Only after that's done is it then able to repaint the screen, at which point you'll finally see the second image.

As Campbell indicated, you need to look into using Timer to avoid this.
Gene Falk
Greenhorn

Joined: Apr 30, 2014
Posts: 4
Thank you for your response.

All the examples I could find on the web using Timer seemed quite complex.
Can you send a code fragment.

I did try the following but the results were the same.

I replaced

try { Thread.sleep( time ); }
catch ( InterruptedException oops){ }

with

try { TimeUnit.SECONDS.sleep(1000); }
catch (InterruptedException oops) {}

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You're welcome The example in the Timer link I posted looked quite simple. What about this link?
Gene Falk
Greenhorn

Joined: Apr 30, 2014
Posts: 4
I don't think the problem has been found.

I took out the sleep, did not add a timer, but put in with a timing loop.

for (int i = 1 ; i < loopCount; i = i + 1)
{
for (int j = 1 ; j < loopCount; j = j + 1)
{
Math.sqrt (i);
}
}

The result is the same, after a delay of a few seconds, picture 2 is displayed.
Picture 1 never shows up.
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 96
Gene Falk wrote:I don't think the problem has been found.


Have you tried using the Timer class instead of Thread.sleep() as Campbell suggested? If done right that will solve your problem.
 
Consider Paul's rocket mass heater.
 
subject: Different behavior after mouse click.