Always use Swing Timer for GUI related tasks, as in javadoc mentioned:


  In general, we recommend using Swing timers rather than
  general-purpose timers for GUI-related tasks because Swing timers all
  share the same, pre-existing timer thread and the GUI-related task
  automatically executes on the event-dispatch thread. However, you
  might use a general-purpose timer if you don't plan on touching the
  GUI from the timer, or need to perform lengthy processing. 


After reading the tutorial you will understand what I did:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class TimerDemo extends JFrame {

    private static final long serialVersionUID = 1L;
    private javax.swing.Timer timer;

    public TimerDemo() {
        super("Panel Timer Demo");
        //initialing swing timer
        timer = new javax.swing.Timer(100, getButtonAction());
        JPanel p = new JPanel ();
        p.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseEntered(MouseEvent ev){
                timer.start();
            }
            @Override
            public void mouseExited(MouseEvent ev){
                timer.stop();
                }
            });

          p.setBackground(Color.blue);

          getContentPane().add(p,BorderLayout.CENTER);
          setDefaultCloseOperation(3);
          setLocationRelativeTo(null);
          pack();
    }

    private ActionListener getButtonAction() {
        ActionListener action = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Heloo");
            }
        };
        return action;
    }

    public static void main(String... args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TimerDemo().setVisible(true);
            }
        });
    }
}


How to use MouseListener event
i created application in that it will call preference from main activity based on  menu option
it is done using  Explicit intent  but it not working . when clicking the menu item
calling activity

public boolean onCreateOptionsMenu(Menu menu)
{
    super.onCreateOptionsMenu(menu);

    menu.add(0,  Menu.FIRST+1, Menu.NONE, "Refresh");
    menu.add(0, Menu.FIRST+3, Menu.NONE, "Set Preferences");

    return true;
}

public boolean onOptionItemSelected(MenuItem item)
{
    switch(item.getItemId()) 
    {
        case Menu.FIRST+1:
            Intent intent =new Intent(this,userpreferences.class);
            startActivityForResult(intent,this.USER_PREFERENCES);
            return true;

        case Menu.FIRST+3:

            return true;    
    }

    return false;
}


preference xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    &lt;CheckBoxPreference
        android:key="AUTO_UPDATE" android:title="update automatically"
        android:summary="update automatically" android:defaultValue="true" /&gt;
    &lt;ListPreference android:key="MINIMUM_MARK" android:title="minimum mark"     
        android:summary="enter the minimum mark of the student" 
        android:defaultValue="30" android:entries = "@array/minimumMarks"   
        android:entryValues="@array/minimum_mark_values" 
        android:dialogTitle="Check the minimum mark required" /&gt;
&lt;/PreferenceScreen&gt;


Preference activity

package com.xml.parse;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class userpreferences extends PreferenceActivity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}


some body plz tell me to do better or correct way

Regards,

Kariyachan
