Solved

Animated button

Posted on 2010-11-22
9
675 Views
Last Modified: 2012-05-10
I would like to have an animated button like the attached image.
This is from TextEdit in OS X.
I don't care about the shape just the animation (the blue button)
(Though the shape is probably part of the System LAF)
Obviously you can't see the animation from a screenshot.
It smoothly transitions between lighter and darker blue


TextEdit-animatedButton.png
0
Comment
Question by:allelopath
  • 6
  • 2
9 Comments
 
LVL 7

Accepted Solution

by:
rodness earned 334 total points
ID: 34190440
Note that this is specific to Apple's LAF.  You can't easily get that effect on any other platform.

If you use a JOptionPane and use the constructor that lets you specify the initially selected button, then the selected button will do that glow effect.  I don't know what's set on the button to actually make it do that.
http://download.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)

I'm not sure how to actually accomplish this on a regular JButton though.
0
 
LVL 7

Assisted Solution

by:rodness
rodness earned 334 total points
ID: 34190596
Ah, I figured it out.

This is going to make it into my own bag of tricks. :)
JButton button = // ... create and set up your JButton

button.requestFocus();

JRootPane root = SwingUtilities.getRootPane( button );
if ( root != null ) {
   root.setDefaultButton( button );
}

Open in new window

0
 
LVL 16

Assisted Solution

by:Valeri
Valeri earned 166 total points
ID: 34195264
You have to create your own subclass of JButton and to implement MouseListener in order to catch MouseEntered ad MouseExited events, and to fade from one color to another.
public class FadeButton extends JButton implements MouseListener {
private Color bgClr = new Color(200, 200, 200);
private Color overClr = new Color(255, 100, 100);
public void mouseEntered(MouseEvent e)  {
// fade from bgClr to overClr
}
public void mouseExited(MouseEvent e)  {
// fade from overClr to bgClr
}
}

Here is example how to achieve fade effect :
http://www.java2s.com/Code/Java/Advanced-Graphics/Fadingbutton.htm
But you have to "fade" from one color to another so, you have to have your own algorithm something like that :
Color(0, 0, 0) -> Color(0, 0, 0 + 30) -> Color(0, 0, 0 + 60) -> Color(0, 0, 0 + 90) .... -> Color(0, 0, 0 + 255)
this fade from BLACK to BLUE during Timer delays.
0
 
LVL 1

Author Comment

by:allelopath
ID: 34200936
I have a tangential question. Valeri points to some handy code on the java2s site but it has copyright protection all over it. How can I use this code, be it legally or in good conscience, with this? ie what's the point of posting code no one can use?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Expert Comment

by:rodness
ID: 34201018
Read it:

 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *

This means you can use it freely, as long as you abide by:


 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.

Keep this comment in the source code.

 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.

If you only distribute a binary, put this comment in a readme file.

 *   * Neither the name of the TimingFramework project nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.

Don't use the names of the people who wrote this in your documentation.

It's legal, and as long as you keep the comment in your source, you have nothing to feel guilty about.
0
 
LVL 7

Expert Comment

by:rodness
ID: 34201034
ps-

Valeri's code example will work, but you can save yourself a lot of work if you just use JButton.requestFocus().

That way they system LAF will do it all for you.  You'll get the glow effect on a mac, and anything else appropriate on other systems.  For free.
0
 
LVL 7

Expert Comment

by:rodness
ID: 34201052
Oh yeah... if you haven't, you need to enable the system look and feel.  Do this up in main():
try {

	UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

} catch ( Exception e ) {

	e.printStackTrace();

}

Open in new window

0
 
LVL 16

Expert Comment

by:Valeri
ID: 34203309
@allelopath : "copyright protection" is inder conditions that rodness mentioned above.
@rodness : it's interesting, I'll try that! But the other systems SYSTEM L&F's will not support that... so the effect will work only on mac machines...
0
 
LVL 7

Expert Comment

by:rodness
ID: 34205895
@valeri:

1) True, but he didn't specify multiple target platforms.

2) this way you get the glow on the Mac and whatever is natively appropriate elsewhere, and

3) it is a butt load less work this way...  The more java can do for you the better.  The more you write, the more you have to debug :)

-r
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now