Win a copy of Java Challengers 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Help with Timers!

 
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 attempting to make a simple program with a label, a button and a timer. I'm trying to attach the label to the timer, and change the location of the label every second in single downward movements. Here is my code for the timer, but im having trouble getting the whoole thing to even run! am i missing any parts here?

javax.swing.Timer Timer1 = new javax.swing.Timer(100, new ActionListener() {

public void actionPerformed(ActionEvent e) {

x = lbl1.getLocation(x);
y = lbl1.getLocation(y);


lbl1.setLocation(int x, int y++)

}
});
so as you can see lbl1 is the label, and i have a button to begin the timer. Whats missing or whats wrong with my code for locating and relocating the label? many thanks in advance.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from syntax errors, ask yourself what the difference is between ++y and y++. Do you know the distinction between side effect and value?
 
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
i dont know the difference i have only used y++ before, please do advise!
as for side effect and value, once again you will have to explain if you will exactly what you mean?
Ive been stuck for days just dont know what to do with myself! Thanks jeff
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The side effect of both y++ and ++y is to increment variable y by one. As for the value of each expression, the easiest way to discover the value yourself is to write a little code:Back to your code: do you really need to increment y or is expression y+1 good enough?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, the code you posted will not compile, so obviously it won't run either. In particular, look at this line of code:

This is a method call. However, in a method call, you should never use type names such as "int" with the parameters. If you remove that, then your code should compile.

Second, you should use the search utility here at the Ranch to find out what the difference between y++ and ++y is. You should be sure to search this forum, the beginner forum, and the SCJP forum. This topic has been discussed on many occassions here. I'm sure google will provide information as well.

HTH

Layne
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few other hints:
  • getLocation: there are two versions of getLocation, but neither takes an int. You might want to check out methods getX and getY.
  • setLocation: when you invoke a method you don't have to repeat the parameters' types: so it is lbl.setLocation(u, v), not lbl.setLocation(int u, int v)
  • moving components: problematic because that's the job of the container's layout manager. The easiest fix is to set the layout manager to null, then you have to set the bounds (x, y, w, h) of the container's components. If you want to see the result of changing the label's bounds, you should call its container's repaint method.


  • [ January 11, 2006: Message edited by: Jeff Albrechtsen ]
     
    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
    Ok thanks for the clarification, ive been looking at the getX getY methods and seen that they take ints.
    im using netbeans to do this program, aloong with its gui designer. I have set the layout to null.
    How do i go about changing the bounds of the label?
    thanks a lot guys
     
    Jeff Albertson
    Ranch Hand
    Posts: 1780
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Jon Thomspson:
    ive been looking at the getX getY methods and seen that they take ints.
    im using netbeans to do this program, aloong with its gui designer. I have set the layout to null.
    How do i go about changing the bounds of the label?


  • getX and getY don't *take* an int, they *return* an int, right?
  • You can set bounds with setBounds, or by separately calling setSize and setLocation.

  • reply
      Bookmark Topic Watch Topic
    • New Topic