Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: JSLider with ChangeListener question

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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
    }
    }
    }

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    fatboyhd2k (June 28th, 2012)

  4. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default 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.

  5. The Following User Says Thank You to Tjstretch For This Useful Post:

    fatboyhd2k (June 28th, 2012)

  6. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by fatboyhd2k; June 28th, 2012 at 03:48 PM.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

     
    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));
     
    		}
    	}
     
    }

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    Last edited by Norm; June 28th, 2012 at 07:46 PM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. problems with customising a JSlider
    By Harry Blargle in forum AWT / Java Swing
    Replies: 3
    Last Post: April 1st, 2012, 01:24 PM
  2. How to Use a JSlider - Java Swing
    By neo_2010 in forum Java Swing Tutorials
    Replies: 4
    Last Post: March 29th, 2010, 09:33 AM
  3. How to Use a JSlider - Java Swing
    By neo_2010 in forum Java Code Snippets and Tutorials
    Replies: 4
    Last Post: March 29th, 2010, 09:33 AM