Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Multiple actions on timers anyone?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, im trying to put two actions on within a timer, one that moves a label across the frame, and one label that stores a variable coutning upwards.

At the moment, i cant get them two work together.
They work fine on theyre own, but when they are both in the timer, one of them just freaks out and shakes.

any ideas what the problem is? thanks in advance
[ January 15, 2006: Message edited by: Jon Thomspson ]
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
works OK for me, using swing JLabels and swing timer
 
Jon Thomspson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm when i // out the code to make the counter, the other animation runs fine, but still when theyre both in the timer its a no go. any idea what the prob might be? they are on the same jframe. thanks
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
perhaps if you posted a demo program of how you are doing it
(just a frame, couple of labels, and the timer code)
 
Jon Thomspson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javax.swing.Timer TimerOne = new javax.swing.Timer(10, new ActionListener() {

public void actionPerformed(ActionEvent e) {

x = lbl1.getX(); // movement code
y = lbl1.getY(); //

lbl1.setSize(159,105); //
lbl1.setLocation(x+1, y+1); //

i = i+ 1; //count code
lblcount.setText(Integer.toString(i + 1));
}
});
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the posted code works OK for me.

if you have many components, perhaps images, the timer interval of 10ms
could be the problem. try it at 1000ms, then 100ms and see if the repainting is OK

you could also try changing some of the code to this


[ January 15, 2006: Message edited by: Michael Dunn ]
 
Jon Thomspson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ive tried that code and still if the counter is being displayed, the label wont move! could it be to do wiith their positioning or one of their properties? thanks
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could it be you have 2 lbl1's declared?
the one added to the frame/panel being different
to the one the timer is moving.

you might have to post a full working demo program
that we can copy/paste/compile/run and observe the problem
 
Jon Thomspson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.awt.event.*;
import java.awt.Event.*;
import java.util.Timer;
import javax.swing.*;
import java.awt.*;
import java.util.*;




public class ASS extends javax.swing.JFrame {


public ASS() {
initComponents();

}

public int x,y,i;

javax.swing.Timer timerone = new javax.swing.Timer(1000, new ActionListener() {

public void actionPerformed(ActionEvent e) {

x = lbl1.getX(); //get x value for label
y = lbl1.getY(); //get y value for table

lbl1.setSize(159,105); //set size of label
lbl1.setLocation(x+1, y+1); //Set location of label +

// i = i+ 1;
// lblTime.setText(Integer.toString(i + 1));



}
});


COde for start button

Mars.start();
Mars.setDelay(50);

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ASS().setVisible(true);
}
});

Theres the code im using a gui designer for the labels and button etc. cheers
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't copy/paste/compile/run and 'observe' with the code you've posted.

here's a simple demo program that does what you want
(perhaps you can spot a difference between the two)

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic