5

I am using the following code to fade-in a JDialog with a javax.swing.Timer:

    float i = 0.0F;
    final Timer timer = new Timer(50, null);
    timer.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (i == 0.8F){
                timer.stop();
            }
            i = i + 0.1F;
            setOpacity(i);
        }
    });
    timer.start();

The Dialog is nicely faded-in with the desired effect but at last, an IllegalArgumentException Occurs saying that:

 The value of opacity should be in the range [0.0f .. 1.0f]

But the problem is I am not going far fro i = 0.8F so how can it be a illegal argument??
Exception occur at line : setOpacity(i);

Any suggestions? Solutions?

Asif
  • 4,980
  • 8
  • 38
  • 53
  • 1
    I suggest to review your code as the `i` you are modifying is probably not the one you expect. It cannot be the `final float i = 0.0F;` variable as that one has been made `final` – Robin Apr 24 '12 at 05:43
  • sorry for that @Robin, its just a typing mistake of mine, edited this time..as you see if i made it final than it throw a `compiler error` rather than `exception`..thanks for pointing it out.. :) – Asif Apr 25 '12 at 07:04
  • Can I ask what is this "setOpacity" method? I'm trying to implement dialog fadein/out too. – Aurelien Ribon Jul 15 '12 at 09:03
  • @AurélienRibon Offcourse you can ask..[`setOpacity(float)`](http://docs.oracle.com/javase/7/docs/api/java/awt/Frame.html#setOpacity(float)), it is a method (in Java SE 7) of `java.awt.Frame`, super-class of `JFrame`.. – Asif Jul 16 '12 at 14:02
  • Oh I see, thanks. I was always trying to maintain a 1.6 compatibility for all my open-source apps, so I never saw this method, but I guess enforcing 1.7 can't really harm users. Plus when Java8 wil come I'll be the first to jump on lambda expressions :) Thanks. – Aurelien Ribon Jul 16 '12 at 16:15

1 Answers1

8

Your problem is that you're dealing with floating point numbers and == doesn't work well with them since you cannot accurately depict 0.8 in floating point, and so your Timer will never stop.

Use >=. Or better still, only use int.

i.e.,

int timerDelay = 50; // msec
new Timer(timerDelay, new ActionListener() {
    private int counter = 0;

    @Override
    public void actionPerformed(ActionEvent e) {
        counter++;
        if (counter == 10){
            ((Timer)e.getSource()).stop();
        }
        setOpacity(counter * 0.1F);
    }
}).start();
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    Wow! thats it...easy and useful..thanks very much! Well one thing more, is it legal to do this thing? I mean using Timer and fading-in dialog, i hope it is not going against standard Swing coding standards? – Asif Apr 24 '12 at 02:20
  • I don't know of any "standards" with regards to this, or any Swing standards for that measure, just caveats with regards to threading, and I don't see this as stepping on threads. – Hovercraft Full Of Eels Apr 24 '12 at 02:21
  • yes my concern is with swing Threading Model and EDT things, I hope its not disturbing them,because i like this little tweak of mine very much :-) – Asif Apr 24 '12 at 02:24
  • See [this](http://stackoverflow.com/questions/5257166/java-floats-and-doubles-how-to-avoid-that-0-0-0-1-0-1-0-9000001) discussion how to do addition for correct float precision – mprabhat Apr 24 '12 at 02:29
  • 1
    @mprab: all he needs here though is to use ints. – Hovercraft Full Of Eels Apr 24 '12 at 02:30
  • @HovercraftFullOfEels i am using `float` with `>=` sign, as with `int` there will be only `8` opacity levels, which is kind of less in fading-in effect.. – Asif Apr 25 '12 at 07:07
  • @Asif: What I suggest though for count accuracy is to use ints as your loop counter and then use the int to create the float inside the loop for your opacity parameter. You can't beat the accuracy of an int. – Hovercraft Full Of Eels Apr 25 '12 at 11:41
  • @HovercraftFullOfEels you forgot `i` here. . .please do edit considering `i` too – Asif Apr 25 '12 at 14:20