Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2006

Tip: Looking for answers? Try searching our database.

How to shake a window as MSN or Yahoo Messenger does

Thread view: 
keepiss@googlemail.com - 27 Apr 2006 17:50 GMT
Hi,

How to shake a window as MSN or Yahoo Messenger does when someone typed
a message to you?
Bart Cremers - 28 Apr 2006 07:46 GMT
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Random;

/**
* @author Bart Cremers (bce1716)
* @since Apr 28, 2006
*/
public class ShakeFrame extends JFrame implements Runnable {

   private static final int SHAKES = 25;
   private static final int SHAKE_FORCE = 5;
   private static final Random RND = new Random();
   private JButton button;

   @Override
   protected void frameInit() {
       super.frameInit();
       JPanel pane = new JPanel();
       pane.setBorder(BorderFactory.createEmptyBorder(50, 50, 50,
50));
       setContentPane(pane);

       button = new JButton("Shake Me !");
       add(button);

       button.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               button.setEnabled(false);
               new Thread(ShakeFrame.this).start();
           }
       });
   }

   public void run() {
       final Rectangle origRect = ShakeFrame.this.getBounds();
       for (int i = 0; i < SHAKES; i++) {
           SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                   int x;
                   if (RND.nextBoolean()) {
                       x = SHAKE_FORCE;
                   } else {
                       x = -SHAKE_FORCE;
                   }

                   int y;
                   if (RND.nextBoolean()) {
                       y = SHAKE_FORCE;
                   } else {
                       y = -SHAKE_FORCE;
                   }

                   ShakeFrame.this.setBounds(origRect.x + x,
origRect.y + y, origRect.width,
                                             origRect.height);
               }
           });
           try {
               Thread.sleep(10);
           } catch (InterruptedException e) { }
       }

       ShakeFrame.this.setBounds(origRect); // Reset to original
position
       button.setEnabled(true);
   }

   public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               JFrame frame = new ShakeFrame();
               frame.pack();
               frame.setLocationRelativeTo(null);
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setVisible(true);
           }
       });
   }
}
keepiss@googlemail.com - 28 Apr 2006 10:31 GMT
Thanks.

Sorry, I did not make it clear. What I want is to shake or blink the
small tag in the taskbar.

Any idea?
Bart Cremers - 28 Apr 2006 10:46 GMT
That's even easier, but it depends a bit on the underlying platform
(JVM).

Simply call JFrame.setVisible(true) whenever you want the blinking to
occur. If the window is not the top window, the taskbar button will
start blinking.

Bart
keepiss@googlemail.com - 28 Apr 2006 14:51 GMT


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.