Solved

Animated button

Posted on 2010-11-22
9
681 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

630 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