Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

2 JSpinners has my head spinning!  RSS feed

 
Todd Bruner
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello:

I'm building a small app that utilizes 2 JSpinners to allow input of a start time and a stop time. I've created an inner class that implements ChangeListener. Here's the code snippet of stateChanged:


This works and the new values are printed out to the terminal. What I need to know however, is which spinner is was changed, Start or Stop.

By this point, you can probably tell I'm very new to Java. You also might be wondering, why both spinners are going to the same ChangeListener. Good Question, it seems wrong to me to create 2 anonymous classes, but what do I know?!? I was hoping to do something like the following within the ChangeEvent:



Can anyone point this lost newbie in the right direction?

Thanks in advance!
Todd
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ August 23, 2006: Message edited by: Michael Dunn ]
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You are not using two anonymous classes?
I have just had discussions on that point with somebody else on this forum here. There is nothing wrong with having two anonymous inner classes, otherwise you will accumulate if elses and you code becomes difficult to understand.

You will notice in the post I quoted that I say you should use a Listener class if you have two Listeners doing something similar. That is exactly what you have here. I suggest you create a class by implementing the ChangeListener interface, and pass details of the button to that.

NB: You have to put that private class inside the other class. If you put it in a file of its own, don't give it private access.

I don't know whether you will have to retain the cast. Try the above and also:-
BTW: Don't use == operators with String objects, unless you specifically want to know whether they are both the same object. You are usually better using the equals() method of the String class.

CR
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do need the cast. I go this to work:
Start Spinner now has the value of -1
Start Spinner now has the value of -2
Start Spinner now has the value of -3
Start Spinner now has the value of -4
Start Spinner now has the value of -3
Start Spinner now has the value of -2
Start Spinner now has the value of -1
Start Spinner now has the value of 0

[ August 24, 2006: Message edited by: Campbell Ritchie ]
 
Todd Bruner
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the excellent replies and pointing me on the right path!

Todd
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a pleasure to help
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!