javaswingjbuttonjslider

Java: Adding a "accept" button for a slider


I am trying to add a button for my slider. What i want is when i click the button, the value of the slider will be saved in a variable. But i am always getting and error while using the ActionListener. Can anyone help please. Here is the code for my slider:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.util.Hashtable;

public class SliderSample {

    public static void main(final String args[]) {
        Runnable runner = new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Sample Sliders");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                ChangeListener listener = new SliderChangeListener();

                JSlider js4 = new JSlider(1,5);
                Hashtable<Integer, JComponent> table = new Hashtable<Integer, JComponent>();
                table.put(1, new JLabel("1"));
                table.put(2, new JLabel("2"));
                table.put(3, new JLabel("3"));
                table.put(4, new JLabel("4"));
                table.put(5, new JLabel("5"));
                js4.setLabelTable(table);
                js4.setPaintLabels(true);
                js4.addChangeListener(listener);
                frame.add(js4, BorderLayout.CENTER);
                frame.setSize(400, 300);
                frame.setVisible(true);
            }
        };
        EventQueue.invokeLater(runner);
    }

    public static class SliderChangeListener implements ChangeListener {
        public void stateChanged(ChangeEvent changeEvent) {
            Object source = changeEvent.getSource();
            JSlider theJSlider = (JSlider)source;
            if (!theJSlider.getValueIsAdjusting()) {System.out.println ("Slider changed: " +         theJSlider.getValue());
            }
        }
    }
}

Solution

  • "this" cannot be used at this point, because you are addressing the Runnable you are in. Change it like this:

    public class SliderSample {
        private JSlider js4 = new JSlider(1, 5);
        private int state = js4.getValue();
    
        public static void main(final String args[]) {
             SwingUtilities.invokeLater(new Runnable() {
                  @Override
                  public void run() {
                      new SliderSample();
                  }
             });
        }
    
        public SliderSample() {
            JFrame frame = new JFrame("Sample Sliders");
            ...
    
            JButton b = new JButton("Save");
            b.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    state = js4.getValue();
                    System.out.println("new state: " + state);
                }
            });
    
            Hashtable<Integer, JComponent> table = new Hashtable<Integer, JComponent>();
            ...
            frame.setVisible(true);
    }