Solved

Animated button

Posted on 2010-11-22
9
678 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 214
servlet doXXX methods 3 62
ejb wildfly example 2 29
Desingning Refactoring existing code 2 26
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…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

821 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