Solved

Animated button

Posted on 2010-11-22
9
679 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jsp insert to database example 2 89
web project error add remove 1 83
Java List 4 76
Which is best Image Resizing Web service 11 40
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

734 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