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

