0

I'm trying to display an image (iconLabel4) for 5 seconds and then display another image (imageLabel) on top of it after. Why doesn't the code work as intended?

When I run it, what happens is this: I press the button "Bathe" and nothing happens.

I would appreciate any help! Thank you.

Code:

JButton bathe = new JButton("Bathe");
bathe.setBounds(370, 450, 80, 25);

bathe.addActionListener(
        new ActionListener() {
            public void actionPerformed(ActionEvent event){
                long startTime=System.currentTimeMillis();

                String actionCommand = event.getActionCommand();

                if (SHOW_ACTION.equals(actionCommand)){
                    while (System.currentTimeMillis() - startTime < 5000) {
                        iconLabel4.setVisible(true);
                    } }
                iconLabel4.setVisible(false);
                imageLabel.setVisible(true);

                repaint();
            };
        });
bathe.setActionCommand(SHOW_ACTION);
panel.add(bathe);
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41

2 Answers2

2
  1. Don't use a while loop for timing. Use a javax.swing.Timer. See more at How to Use Swing Timers. Here's the basic construct

    Timer (int delayInMillis, ActionListener listener)
    

    where delayInMillis is the millisecond delay between "ticks". Every tick will fire an ActionEvent, just like a button press would. So the listener you pass to the timer, had the actionPerformed method that will be called every tick.

  2. Just use one JLabel and two ImageIcons. When you try to set component visible and not visible after the containter is already visible, you need to revalidate() and repaint() the container. That's not the correct approach though. Just use one label, and make use of the method JLabel.setIcon(ImageIcon) when you want to change the icon.

  3. Don't use null layouts. Learn to use layout managers and let them do the positioning for you. See more at Laying out Components Within a Container

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Huh.. I had to up vote based on a quick glance that included point 3, but then I noticed your artful inclusion of code (formatting) in a list. That is something I always have trouble with. Nice work! – Andrew Thompson May 28 '14 at 02:38
  • 2
    @AndrewThompson 8 spaces instead of 4 :-). Oh and to continue the paragraph point after the code, just go to next line, don't skip a line :-) – Paul Samsotha May 28 '14 at 02:42
  • *"8 spaces instead of 4"* Aha! *"(after) don't skip a line* Got it. Thank you. :) – Andrew Thompson May 28 '14 at 03:29
0

Just a suggestion, you can use Thread.sleep(5000).

Considerations: Thread.sleep() is inaccurate. How inaccurate depends on the underlying operating system and its timers and schedulers. I've experienced that garbage collection going on in parallel can lead to excessive sleep. - StackOverflow

But if you don't mind a few milliseconds here and there, its easier.

Community
  • 1
  • 1
nom
  • 256
  • 3
  • 16