Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Animating a JLabel and a JSlider  (Read 2361 times)
0 Members and 1 Guest are viewing this topic.
Offline demonknight483

Innocent Bystander





« Posted 2005-11-25 22:05:40 »

Im not sure if anyone can help out here but its worth a shot. In the program im writing we are using jogl for animation and as the anmiation is running we would like to have in our main GUI window the ability to use a JLabel as an animation clock to display the correct times of the animation as well as have a JSlider as a progress bar, with the intention of the user being able to move the JSlider to a position and have the animation go forward and back in time. I wrote a small little app to test how I could do this but it doesnt work. I was basically having a for loop to run through the min and max time and every time the for loop would increment i would call the setText() function of the JLabel and the setValue() function of the JSlider to have the values incremented but when i click the button to accomplish it it never updates the labels or the slider until the program is done running through the for loop. Can anyone help me?
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #1 - Posted 2005-11-26 02:06:17 »

Sound like you are running the loop on the AWT event thread - the same thread that processes the repaint events for the label and slider.  Because you never return from processing the button press event, those repaint events don't get a chance to run until the loop is over.

There are two basic ways to fix it:

- Have the button start a swing timer and increment the positon when the timer fires.

or...

- Create another thread to handle the changing of the position, and use SwingUtilities.invokeLater to change the label and slider.

Offline Jeff

JGO Coder




Got any cats?


« Reply #2 - Posted 2005-11-26 03:47:07 »

OR  your just failing to call repaint()

Every time you change a swing widget's display you need to call <widget>.repaint() to let it know it needs to update Cool


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #3 - Posted 2005-11-26 23:15:19 »

Um, Jeff, that's not entirely true.  I'm suprized you suggested it without more qualifications.

Most Swing components will trigger the repaint event internally when you do things like setText("hi mom") on a JLable or setPosition(42) on a JSlider for example.

The code for JLabel's setText method contains:
1  
2  
3  
4  
5  
6  
7  
8  
9  
    public void setText(String text) {

        ...

        if (text == null || oldValue == null || !text.equals(oldValue)) {
            revalidate();
            repaint();
        }
    }

Offline Jeff

JGO Coder




Got any cats?


« Reply #4 - Posted 2005-11-27 00:03:11 »

Guess I just assume they wont because I have run into them not repainting themselves too often.

Its the safest course though in this case you are correct.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline cylab

JGO Ninja


Medals: 43



« Reply #5 - Posted 2005-11-27 12:53:35 »

+1 for AWT Event Thread problem.

I assume you have something like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
        final JLabel label= new JLabel();
        final JButton button= new JButton();
        button.addActionListener(
          new ActionListener()
          {
            public void actionPerformed(ActionEvent e)
            {
              for(int i=0; i < 100; i++)
              {
                label.setText("...");
              }
            }
          }
        );

This way, Swing/Awt never gets a chance to act upon the desired changes.

Since Swing and multithreading is a complicated matter, you have to write this code like
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
        final JLabel label= new JLabel();
        final JButton button= new JButton();
        button.addActionListener(
          new ActionListener()
          {
            public void actionPerformed(ActionEvent e)
            {
              new Thread()
              {
                public void run()
                {
                  for(int i=0; i < 100; i++)
                  {
                    SwingUtilities.invokeLater(
                      new Runnable()
                      {
                        public void run()
                        {
                          label.setText("...");
                        }
                      }
                    );
                  }
                }
              }.start();
            }
          }
        );

So you have the loop outside the AWT event thread and the setText()-calls injected back in the Event queue via SwingUtilities.invokeLater().

You might split the above code up in declared classes instead of inline declarations to make it more readable.
Just calling repaint on the label might work as well, but is not the recommended way of dealing with such kind of things.

Mathias - I Know What [you] Did Last Summer!
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #6 - Posted 2005-11-27 17:47:55 »

Just for the record setText() is one of the few Swing methods that is guaranteed to be safe to call from outside the event thread.  Not that it helps in this case, since there is that slider to update as well.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (22 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (26 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!