0

Im very new to swing, but I tried making this program which creates a slider and a label. As the slider is moved (form 1 to 16), the label changes. However, my label doesn't get updated, and instead I get thread exceptions and other errors when I slide the slider. Here is the full code:

package edu.cuny.brooklyn.cisc3120;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.*;

public class Gui extends JFrame {
    private static final int limit = 4;
    private static final int minInteger = 1;
    private static final int maxInteger = 16;

    private static JLabel currentGuess;
    private static JSlider slider;

    public Gui()
    {
        setLayout(new FlowLayout());

        JSlider slider = new JSlider(JSlider.HORIZONTAL, minInteger, maxInteger, 1);
        add(slider);

        slider.setMajorTickSpacing(1);
        slider.setPaintLabels(true);
        slider.setPaintTicks(true);

        currentGuess = new JLabel("Current Guess: 1");
        add(currentGuess);

        SliderEvent e = new SliderEvent();
        slider.addChangeListener(e);
    }

    public static class SliderEvent implements ChangeListener
    {
        public void stateChanged(ChangeEvent e)
        {
            currentGuess.setText("Current Guess: " + slider.getValue() );
        }

    }

    public static void main(String[] args)
    {
        Gui guessingGame = new Gui();
        guessingGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guessingGame.setTitle("Guessing Game");
        guessingGame.setLocation(100, 100);
        guessingGame.pack();
        guessingGame.setVisible(true);
    }
}

I reiterate that i'm very new to swing. So if I'm making some stupid mistake, go easy on me please.

arslan sana
  • 101
  • 1
  • 8
  • *"I reiterate that i'm very new to swing."* Making cross-platform GUIs is an advanced topic, and you really should get used to understanding and solving exceptions before getting into them. See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) Another tip: Always copy/paste error and exception output! – Andrew Thompson Nov 05 '16 at 21:32

1 Answers1

1
private static JSlider slider;

    public Gui()
    {
        setLayout(new FlowLayout());

        JSlider slider = new JSlider(JSlider.HORIZONTAL, minInteger, maxInteger, 1);

You are defining the slider twice, once as an instance variable (which is null) and once as a local variable.

Get rid of the local variable:

//JSlider slider = new JSlider(JSlider.HORIZONTAL, minInteger, maxInteger, 1);
slider = new JSlider(JSlider.HORIZONTAL, minInteger, maxInteger, 1);

Also, you should NOT be using all those static variables. That is an indication of a poor design. I suggest you look at the section from the Swing tutorial on How to Use Sliders for demo code that will show a better program structure. Download the demo code an modify it for your requirements.

camickr
  • 321,443
  • 19
  • 166
  • 288