Hello I would like to implement multiple swing timers, each of which have a different function, but I cannot seem to separate the functions. I have one actionlistener and I cannot make another one. Please help.
Hello I would like to implement multiple swing timers, each of which have a different function, but I cannot seem to separate the functions. I have one actionlistener and I cannot make another one. Please help.
@ sirlink99 wrote
I would like to implement multiple swing timers
yes good idea, is possible
each of which have a different function
yes good idea, probably safer way
but I cannot seem to separate the functions. I have one actionlistener and I cannot make another one
from ActionListener you can call Timer (I hope that we speak about java.swing.Timer, because there are also exists java.util.Timer) as 2nd parameter you put only Action (java.swing.Action)
nice idea, but without base (without your code)
much luck
Edited by mKorbel: n/a
here is my code. the actionListener would be one and the delayTime would be another one, but how would I call a timer from the actionlistener?
// The "FractionReaction" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.games4j.webAPI.*;
public class FractionReaction extends Applet implements KeyListener, ActionListener
{
Timer timer, wait;
double waitTime;
int screen = 0, select = 1, delay, startBox = 0, time = 0;
double seconds;
Color sqcol = Color.blue;
boolean loggedIn;
// Place instance variables here
public void init ()
{
resize (300, 300);
//timer = new Timer (1, actionPerformed);
//wait = new Timer (1000, delayTime);
wait.start ();
waitTime = Math.random () * 999999000;
delay = (int) Math.round (waitTime);
loggedIn = Highscore.isLoggedIn (applet);
addKeyListener (this);
// Place the body of the initialization method here
} // init method
public void paint (Graphics g)
{
if (screen == 0)
{
if (!loggedIn)
{
g.drawString ("log in to be able to save highscores!!!", 10, 5);
}
g.drawString ("To start press 's'", 120, 50);
}
if (screen == 1)
{
if (startBox == 0)
{
g.setColor (sqcol);
g.fillRect (100, 100, 100, 100);
//for (int i = 0; i < waitTime; i++);
}
else if (startBox == 1)
{
sqcol = Color.red;
g.setColor (sqcol);
g.fillRect (100, 100, 100, 100);
}
g.drawString ("Time: " + time, 10, 10);
}
// Place the body of the drawing method here
} // paint method
public void actionPerformed (ActionEvent e)
{
seconds += 1;
time += 1;
repaint ();
if (seconds == delay)
{
startBox = 1;
repaint ();
timer.start ();
}
}
public void delayTime (ActionEvent e)
{
}
public void keyPressed (KeyEvent e)
{
int key = e.getKeyCode ();
if (key == KeyEvent.VK_SPACE)
{
timer.stop ();
}
if (key == KeyEvent.VK_TAB)
{
sqcol = Color.blue;
waitTime = Math.random () * 999999000;
delay = (int) Math.round (waitTime);
timer.restart ();
//timer.start ();
repaint ();
}
if (key == KeyEvent.VK_S){
screen = 1;
repaint();
}
}
public void keyReleased (KeyEvent e)
{
}
public void keyTyped (KeyEvent e)
{
}
} // FractionReaction class
there is nothing about, but maybe this one can help you
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Junk implements ActionListener {
private NumberFormat nf;
private JLabel lGallons;
private JLabel lCost;
private JPanel p;
private double d = 0;
private double ppg = 2.999;
private Timer t;
public Junk() {
nf = NumberFormat.getNumberInstance();
nf.setMaximumIntegerDigits(3);
nf.setMinimumIntegerDigits(3);
nf.setMaximumFractionDigits(3);
nf.setMinimumFractionDigits(3);
JFrame f = new JFrame("Junk: JGasPump");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
lGallons = new JLabel(nf.format(d));
lCost = new JLabel(nf.format(d * ppg));
p.add(new JLabel("Gallons:"));
p.add(lGallons);
p.add(new JLabel("Dollars:"));
p.add(lCost);
f.add(p);
f.pack();
f.setVisible(true);
t = new Timer(50, this);
t.setDelay(250);
t.restart();
}
static public void main(String[] args) {
Junk junk = new Junk();
}
@Override
public void actionPerformed(ActionEvent e) {
d += 0.005;
lGallons.setText(nf.format(d));
lCost.setText(nf.format(d * ppg));
if (d >= 1) {
t.stop();
}
}
}
and this one is really good for you
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.Timer;
public class NewsTest extends JFrame {
private static final long serialVersionUID = 1L;
private JTextPane textPane;
private JButton cdButton, clrButton;
private String[] news;
private int[] delay;
private Timer ctTimer, newsTimer;
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new NewsTest().setVisible(true);
}
});
}
public NewsTest() {
initComponents();
news = new String[5];
delay = new int[5];
news[0] = "News #1";
delay[0] = 2000;
news[1] = "News #2";
delay[1] = 4000;
news[2] = "News #3";
delay[2] = 1000;
news[3] = "News #4";
delay[3] = 1000;
news[4] = "News #5";
delay[4] = 3000;
}
private void initComponents() {
setSize(500, 500);
GridLayout gl = new GridLayout(3, 1);
setLayout(gl);
textPane = new JTextPane();
textPane.setEditable(false);
textPane.setVisible(true);
add(textPane);
cdButton = new JButton("Count down");
add(cdButton);
cdButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
activateCountDown();
}
});
clrButton = new JButton("Stop and Clear");
add(clrButton);
clrButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
ctTimer.stop();
} catch (Exception ex) {
}
try {
newsTimer.stop();
} catch (Exception ex) {
}
textPane.setText("");
}
});
}
private void activateCountDown() {
ActionListener ctListener = new ActionListener() {
private int count = 5;
@Override
public void actionPerformed(ActionEvent e) {
textPane.setText("News will begin in ... " + String.valueOf(count));
if (count > 0) {
count--;
} else {
((Timer) e.getSource()).stop();
activateNewsReport();
}
}
};
ctTimer = new Timer(1000, ctListener);
ctTimer.setInitialDelay(0);
ctTimer.start();
}
private void activateNewsReport() {
ActionListener ctListener = new ActionListener() {
private int count = 0;
private String allNews = "";
@Override
public void actionPerformed(ActionEvent e) {
if (count < news.length) {
allNews = allNews + news[count] + "\n\n";
textPane.setText(allNews);
}
count++;
if (count < news.length) {
((Timer) e.getSource()).setDelay(delay[count]);
}
if (count > news.length) {
textPane.setText(allNews + "No more news");
((Timer) e.getSource()).stop();
}
}
};
newsTimer = new Timer(delay[0], ctListener);
newsTimer.setInitialDelay(0);
newsTimer.start();
}
}
I'm sorry I still don't understand could you please elaborate a bit more On the actual timer part?
Edited by sirlink99: n/a
Timer timer_1, timer_2;
timer_1 = new Timer(new TimerAction1());
timer_2 = new Timer(new TimerAction2());
//the 2 timers call different methods
//define the 2 methods
class TimerAction1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//whatever you want the first timer to do
}
}
class TimerAction2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//whatever you want Timer2 to do
}
}
As far as using all the methods for the timer class I would have to see the code to help with that.
hmmm, how long you want to waiting
waitTime = Math.random () * 999999000;
max value for timer is in mileseconds.... that's maybe more than 2month
you are put here some code sniped, that's out of context (for me),
again post here YOUR real code plus you have to DESCRIBE what you are really want to do....
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class DataStreamExample {
public static void main(String[] args) throws IOException {
File f = new File("seminar.txt");
if (f.exists()) {
BufferedReader br ...
There are a number of very old threads on CUDA so I'm starting a new one rather than resurrecting an old one.
Does anyone here have any experience setting up ...
Dear Friends, I am facing a problem, I have two forms,
Form1 Has one DataGrideview and Button redirect to Form2, which has Button(Browse Excel File) and combobox(Sheet No of excel ...