Java Threading
November 21, 2009 · Print This Article
TimerTextField.java
JAVA:
-
import java.util.Date;
-
-
import javax.swing.JTextField;
-
-
-
private boolean gogo = true;
-
@Override
-
public void run() {
-
// TODO Auto-generated method stub
-
Date d;
-
-
while(gogo)
-
{
-
this.setText(d.toString());
-
this.repaint();
-
try {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
}
-
-
public void closeMe()
-
{
-
gogo = false;
-
}
-
}
Apps.java
JAVA:
-
import java.awt.Component;
-
import java.awt.Rectangle;
-
import java.awt.event.ActionEvent;
-
import java.awt.event.ActionListener;
-
import java.util.concurrent.ExecutorService;
-
import java.util.concurrent.Executors;
-
-
import javax.swing.JButton;
-
import javax.swing.JFrame;
-
import javax.swing.JScrollPane;
-
import javax.swing.JTextArea;
-
-
-
-
private TimerTextField ttf;
-
private JTextArea ta;
-
private JScrollPane jp;
-
private JButton jb;
-
public Apps()
-
{
-
-
this.setVisible(true);
-
this.setLayout(null);
-
this.setBounds(10,20,500,400);
-
init();
-
}
-
-
protected void init()
-
{
-
ttf = new TimerTextField();
-
-
-
jb.setText("Click Me");
-
-
-
@Override
-
// TODO Auto-generated method stub
-
jb_method(e);
-
-
}
-
-
});
-
-
}
-
-
{
-
ExecutorService es = Executors.newSingleThreadExecutor();
-
es.execute(ttf);
-
//ttf.run();
-
}
-
-
{
-
this.add(c);
-
c.setBounds(r);
-
-
}
-
-
{
-
new Apps();
-
}
-
}






Comments
Got something to say?