I have two JSliders that are both going to a common stateChanged Method, but I am having trouble separating them. I assumes they were somewhat like separating something in the actionPerformed method, but it doesn't seem to work. it does however go into the method, it is just the if statements that are not working.

public void stateChanged(ChangeEvent e) {
		// TODO Auto-generated method stub
		JSlider source = (JSlider)e.getSource();
		if ("waveSlider1".equals(source)){
			waveLength1 = (int)source.getValue();
			System.out.print("Value Changed to: " + waveLength1);
		}
		if ("waveSlider2".equals(source)){
			waveLength2 = (int)source.getValue();
                        System.out.print("Value Changed to: " + waveLength2);
		}
		System.out.print("Senses Change");
	}

I am having trouble separating them.

Not sure what this means? Your code appears to try to get the source component for the event.
Print out what is returned by the getSource() method. You are assuming that it is a String. Also read the API doc for the getSource method.

What object generated the event that was passed to the listener method?

I have two JSliders and each of them controls a certain aspect of my code, to be specific the frequency of two waves. I am trying to separate the different sliders, so that when they are changed the int I have assigned them updates. I have successfully managed to get it into the stateChanged method, I am just having trouble recognizing which one was altered. And I read up on the api and tried to convert it to a string. Nothing changed.

if ("waveSlider1".equals(source)){

You are trying to compare a String object with a JSlider Object - always false.
Assuming waveSlider1 is a variable containing the first JSlider you need to compare the variable, not a String containing the variable's name

if (source == waveSlider1) {

<moderator edit: snipped instead of deleted to retain thread continuity>

mKorbel (or an admin)
I suggest you delete the previous post a.s.a.p. - it's copyrighted material that has been posted in violation of the licence agreement, which begins

* Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.

commented: thanks for notice +8

Set the names for sliders
jSlider1.setName("waveSlider1"); ...
and inside stateChanged method simply write
if ("waveSlider1".equals(source.getName())){

@JamesCherrill

excelent point, because I don't knew that is from official tutorial, and I suck that from official java.sun.forum (now OTN),

and for next time check if code does/doesn't contains any changes, before any your next steps :-)

Set the names for sliders
jSlider1.setName("waveSlider1"); ...
and inside stateChanged method simply write
if ("waveSlider1".equals(source.getName())){

@quuba:
How is that better than
if (source == waveSlider1)
?

and for next time check if code does/doesn't contains any changes, before any your next steps :-)

?
Did you expect me to check 527 lines of code against the original to look for changes?
Even if it was changed, it still needs the copyright notice.
(or did I misunderstand you?)

@JamesCherrill

agreed you are right, nothing happens when it'll be there, and I asked moderator(s) for following:

1/ add copyright

or

2/ delete this code

really thanks for notify us

if doesn't any change then don't hesitage to flag my post as Java.Forum.Sun Copyright protected

And my two cents:
@quua

jSlider1.setName("waveSlider1"); ...
and inside stateChanged method simply write 
if ("waveSlider1".equals(source.getName())){

This is a poor technique, typing a String into the code in more than one place. If there is a typo this code won't work but it will compile.
Better is to use a String variable to hold the label and use that variable in your code.

final String SldrLbl = "waveSlider1";
    ....
jSlider1.setName(SldrLbl); ...
and inside stateChanged method simply write 
if (SldrLbl.equals(source.getName())){
Member Avatar for hfx642

I'm just wondering...
If you are using TWO JSliders, why are you not using TWO handlers?
It would solve your problem... wouldn't it?

I'm just wondering...
If you are using TWO JSliders, why are you not using TWO handlers?
It would solve your problem... wouldn't it?

I agree. I always wonder the same thing when I see a handler that has blocks of
if (e.getSource() == firstJComponent) ...
if (e.getSource() == secondJComponent) ...
...
(even worse if it's messing about with Strings to achieve the same thing)

Thanks for the help. I removed the quotes and put in the two equal signs and it works now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.