Un-responsive Timer

Hi I am trying to create a Timer using javax.swing.Timer that starts a mouse event after a delay of 10 seconds. More specifically, when the mouse enters the panel ("panel_1") it should start the timer and do a task in the MouseListener 'mouseEntered' method, (in this case just output the string "starting", after a delay of 10 secs. However, I do not get the desired result. The string "starting" is displayed straight away without any delay. Does anyone have any ideas? The code is below. thanks!

--

import java.awt.*;

import javax.swing.*;

import javax.swing.Timer;

import java.awt.event.*;

import java.net.*;

import java.io.*;

public class ControlPanel_Client extends JFrame implements ActionListener, MouseListener,Serializable

{

long deadline;

long start_time;

long interval = deadline - start_time;

boolean isInside = true;

ControlPanel_Client frame = null;

public static int delay = 10000;

public Timer t;

JPanel Panel_1 = new JPanel();

JPanel Panel_2 = new JPanel();

JPanel Panel_3 = new JPanel();

JPanel Panel_4 = new JPanel();

JPanel Panel_5 = new JPanel();

JPanel Panel_6 = new JPanel();

JPanel Panel_7 = new JPanel();

JPanel Panel_8 = new JPanel();

JLabel x = new JLabel("House");

JLabel j = new JLabel("Ace Burglar Alarm - Sentinel 2000");

JLabel y = new JLabel("***<<Intruder Detected>>***");

JButton b1 = new JButton("On/Off");

JButton b2 = new JButton("Arm");

JButton b3 = new JButton("Disarm");

Socket serv = null;

DataInputStream incoming = null;

DataOutputStream outgoing = null;

ObjectOutputStream o = null;

int id = 0;

public ControlPanel_Client()

{

setLayout(new BorderLayout());

Panel_1.add(x);

add(Panel_1,BorderLayout.CENTER);

add(Panel_2,BorderLayout.SOUTH);

Panel_2.setLayout(new BorderLayout());

Panel_4.setLayout(new BorderLayout());

Panel_5.setLayout(new BorderLayout());

Panel_3.add(b1);

Panel_3.add(b2);

Panel_3.add(b3);

Panel_4.add(j,BorderLayout.WEST);

Panel_8.add(y);

Panel_2.add(Panel_3,BorderLayout.SOUTH);

Panel_2.add(Panel_4,BorderLayout.NORTH);

Panel_2.add(Panel_5,BorderLayout.CENTER);

Panel_5.add(Panel_6,BorderLayout.WEST);

Panel_5.add(Panel_7,BorderLayout.EAST);

Panel_5.add(Panel_8,BorderLayout.CENTER);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

Panel_1.addMouseListener(this);

try{

serv = new Socket("localhost",8000);//create a socket to connect to the server

incoming = new DataInputStream(serv.getInputStream()); //read primitive data from server

outgoing = new DataOutputStream(serv.getOutputStream()); //write primitive data to server

o = new ObjectOutputStream(outgoing); //write object/reference data to server

}

catch(Exception e){

System.out.println(e);

}

}

public static void main(String [] args)

{

ControlPanel_Client frame = new ControlPanel_Client();

//frame.t = new Timer(10000, frame);

frame.setTitle("Burglar Alarm Client");

frame.setSize(300,350);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

frame.Panel_1.setBackground(Color.white);

frame.Panel_8.setBackground(Color.gray);

try{

frame.outgoing.writeChar('S');

}

catch(Exception f){

System.out.println(f);

}

System.out.println("Please enter the address of the house to be monitored: ");

String address = Keyboard.readString();

try{

frame.o.writeObject(address);

}

catch(Exception f){

System.out.println(f);

}

try{

frame.id = frame.incoming.readInt(); //client identification number

System.out.println("the client id number is: " + frame.id);

System.out.println("Client: Server message received " + frame.id);

}

catch(Exception e)

{System.out.println(e);}

}

public void actionPerformed(ActionEvent e)

{

if (e.getSource()==b1)

{

y.setText("Disarmed");

try

{

outgoing.writeChar('O');

outgoing.writeInt(id);

}

catch(Exception j)

{System.out.println(j);}

}

if (e.getSource() == b2)

{

try

{

Thread.sleep(10000);

outgoing.writeChar('A');

outgoing.writeInt(id);

y.setText("Armed");

}

catch(Exception j)

{System.out.println(j);}

}

if (e.getSource() == b3)

{

try

{

outgoing.writeChar('D');

outgoing.writeInt(id);

}

catch(Exception j)

{System.out.println(j);}

}

repaint();

}

public void mouseEntered(MouseEvent e)

{

if(e.getSource()==Panel_1 && y.getText().equals("Armed")) //&& System.currentTimeMillis() > 100000000)

{

t = new Timer(delay, this);

t.setRepeats(false);

t.start();

System.out.println("starting...");

}

repaint();

}

public void mouseExited(MouseEvent e)

{

if (e.getSource()==Panel_1 && y.getText().equals("Armed"))

{

t.stop();

}

}

public void mouseReleased(MouseEvent e)

{

}

public void mouseClicked(MouseEvent e)

{

}

public void mousePressed(MouseEvent e)

{

}

}[/code]

Message was edited by:

prashx

[5860 byte] By [prashxa] at [2007-11-27 4:39:10]
# 1

This code works fine for me.

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.util.Calendar;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.Timer;

public class TimerDemo extends JPanel{

Timer t;

TimerDemo() {

super(new GridLayout(1,1));

add(new JLabel("Timer Demo Panel."));

addMouseListener(new MyMotionListener());

t = new Timer(10000, new TimerActionListener());

}

private static void createAndShowGUI() {

//Create and set up the window.

JFrame frame = new JFrame("TimerDemo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.

TimerDemo newContentPane = new TimerDemo();

newContentPane.setOpaque(true); //content panes must be opaque

frame.setContentPane(newContentPane);

//Display the window.

frame.setSize(200, 200);

frame.setResizable(false);

frame.setVisible(true);

}

public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

class MyMotionListener extends MouseAdapter{

public void mouseEntered(MouseEvent e) {

t.start();

System.out.println(" *** Time: "+Calendar.getInstance().getTime());

}

public void mouseExited(MouseEvent e) {

t.stop();

}

}

class TimerActionListener implements ActionListener{

public void actionPerformed(ActionEvent e) {

if (e.getSource() == t) {

System.out.println("10 seconds after mouseEntered Event.");

System.out.println(" *** Time: "+Calendar.getInstance().getTime());

}

}

}

}

Hope That Helps

java_2006a at 2007-7-12 9:49:42 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks for this! However I still don't know how to apply the method to my program, i.e. using only the MouseListener interface. I am a bit of a newbie.
prashxa at 2007-7-12 9:49:42 > top of Java-index,Desktop,Core GUI APIs...
# 3
Do I need to import any other classes? At the moment my class just implements the ActionListener and MouseListener interfaces
prashxa at 2007-7-12 9:49:42 > top of Java-index,Desktop,Core GUI APIs...
# 4
Hey I got it working! Thanks for the tip!
prashxa at 2007-7-12 9:49:42 > top of Java-index,Desktop,Core GUI APIs...