Solved

Animated button

Posted on 2010-11-22
9
677 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

813 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

11 Experts available now in Live!

Get 1:1 Help Now