JSLider with ChangeListener question
I watched a tutorial yesterday trying to learn more about java programming....and have read several books in the last couple of years and I can't figure this one out.
The tutorial code was:
public class Slider extends JSlider{
public Slider{
JSlider redSlider = new JSlider(SwingConstants.HORIZONTAL,0,255,0);
redSlider.addChangeListener(new listener());
//and the rest of the code
}
public class listener implements ChangeListener{
public void stateChanged(ChangeEvent e){
int r = redSlider.getValue(); //cannot resolve redSlider :confused:
}
}
}
My question is the program compiles and runs on the tutorial, however when I write the code in JAVA7 and Eclipse Indigo I get unable to resolve redSlider in the listener
stateChanged method. I have worn Google out today, read my books and watched other tutorials, I can't figure out what I'm doing wrong. I thought that this would be an inner class and that inner class had access to the enclosing class variables. I really appreciate any help, of course I'm gonna continue looking myself.
Thanks,
Darryl
Re: JSLider with ChangeListener question
If you have some code you are having problems with, please post it. It should be a small, complete program that compiles, executes and shows the problem.
The redSlider variable in your code is not in scope where you are trying to access it. It is defined local to a method.
Re: JSLider with ChangeListener question
Remember to wrap your code with '[code=Java] *CODE* [/code]' tags to make it easier to read your code. Also, please include the entire error message, because it could contain other useful information. Often, errors like these are not the result of that particular line, but mismatched brackets / other subtle errors earlier.
On another note, try to follow the Java Naming Conventions to make it easier too quickly read and understand your code.
Lastly, make a SSCCE [Simple Self-Contained Complete Example] that demonstrates this issue; the code you posted has many, many syntax errors.
Re: JSLider with ChangeListener question
Thanks for the quick reply guys. I was hesitant to post the whole class, I didn't want to be intrusive. Norm you've got the problem, the variable "r" is out of scope, I just don't understand why the tutorial on the net compiles and runs the code but I can't locally. I'll post the whole class today.
Re: JSLider with ChangeListener question
Here's the code, I want to understand what's happening even though this is a toy program. I can compile and run everything except the listener class.
code=java
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class Slider extends JFrame{
public Slider(){
JSlider redSlider = new JSlider(SwingConstants.HORIZONTAL,0,255,0);
JSlider greenSlider = new JSlider(SwingConstants.HORIZONTAL,0,255,0);
JSlider blueSlider = new JSlider(SwingConstants.HORIZONTAL,0,255,0);
JLabel redLabel = new JLabel("Red = 0");
JLabel greenLabel = new JLabel("Green = 0");
JLabel blueLabel = new JLabel("Blue = 0");
Container pane = this.getContentPane();
pane.setLayout(new GridLayout(1,3,3,3));
JPanel sliderPanel = new JPanel(new GridLayout(3,1,2,2));
JPanel labelPanel = new JPanel(new GridLayout(3,1,2,2));
JPanel colorPanel = new JPanel();
colorPanel.setBackground(Color.BLACK);
sliderPanel.add(redSlider);
sliderPanel.add(greenSlider);
sliderPanel.add(blueSlider);
labelPanel.add(redLabel);
labelPanel.add(greenLabel);
labelPanel.add(blueLabel);
pane.add(sliderPanel);
pane.add(labelPanel);
pane.add(colorPanel);
}//end constructor
//I know this part is out of scope but how did the tutorial compile and I can't compile and run the program.
//I can code JSlider source = (JSlider) c.getSource();
//then code source.getValue()
private class sliderListener implements ChangeListener{
public void stateChanged(ChangeEvent c){
int r = redSlider.getValue();
int g = greenSlider.getValue();
int b = greenSLider.getValue();
redLabel.setText("Red = " + r);
greenSlider.setText("Green = " + g);
blueLabel.setText("Blue = " + b);
colorPanel.setBackground(new Color(r,g,b));
}
}
}
/code
Re: JSLider with ChangeListener question
Please Edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
You left off the []s
You forgot to post the full text of the error messages.
Re: JSLider with ChangeListener question
Here's the code again, I've already learned something here. The errors are generated in the listener section, the are "redSlider cannot be resolved", "greenSlider cannot be resolved", and "blueSlider cannot be resolved". Nothing referenced in the listener class can be resolved. Is the listener class not a inner class?
Code java:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class Slider extends JFrame{
public Slider(){
JSlider redSlider = new JSlider(SwingConstants.HORIZONTAL,0,255,0);
JSlider greenSlider = new JSlider(SwingConstants.HORIZONTAL,0,255,0);
JSlider blueSlider = new JSlider(SwingConstants.HORIZONTAL,0,255,0);
JLabel redLabel = new JLabel("Red = 0");
JLabel greenLabel = new JLabel("Green = 0");
JLabel blueLabel = new JLabel("Blue = 0");
Container pane = this.getContentPane();
pane.setLayout(new GridLayout(1,3,3,3));
JPanel sliderPanel = new JPanel(new GridLayout(3,1,2,2));
JPanel labelPanel = new JPanel(new GridLayout(3,1,2,2));
JPanel colorPanel = new JPanel();
colorPanel.setBackground(Color.BLACK);
sliderPanel.add(redSlider);
sliderPanel.add(greenSlider);
sliderPanel.add(blueSlider);
labelPanel.add(redLabel);
labelPanel.add(greenLabel);
labelPanel.add(blueLabel);
pane.add(sliderPanel);
pane.add(labelPanel);
pane.add(colorPanel);
}//end constructor
//I know this part is out of scope but how did the tutorial compile and I can't compile and run the program.
//I can code JSlider source = (JSlider) c.getSource();
//then code source.getValue()
private class sliderListener implements ChangeListener{
public void stateChanged(ChangeEvent c){
int r = redSlider.getValue();
int g = greenSlider.getValue();
int b = greenSLider.getValue();
redLabel.setText("Red = " + r);
greenSlider.setText("Green = " + g);
blueLabel.setText("Blue = " + b);
colorPanel.setBackground(new Color(r,g,b));
}
}
}
Re: JSLider with ChangeListener question
Where are the error messages?
The redSlider, etc variables are defined locally to the constructor and are not known outside of the method. Move their definitions out of the constructor to the class level.