whats wrong with the code? I can make a slider, but the listener is apparently not doing it's job.
here is the class with main method that makes a frame.
import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
public class SliderTester extends JFrame{
public static void main(
String[] args) {
JFrame myWindow = new JFrame();
myWindow.setSize(300,300);
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = myWindow.getContentPane();
SliderPanel s = new SliderPanel();
c.add(s, BorderLayout.SOUTH);
myWindow.setVisible(true);
}
}
and here is the class that makes the slider:
import java.awt.event.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.*;
import javax.swing.*;
public class SliderPanel extends JSlider{
// **** Instance variables for class SliderPanel *****/
JSlider s = new JSlider(0,10,5);
int sValue;
// **** Constructors for class SliderPanel ******/
public SliderPanel() {
s = new JSlider(0,10,5);
SliderListener l = new SliderListener();
s.addChangeListener(l);
}
// ***************** method to display output from slider
public void printState(){
sValue = s.getValue();
System.out.println(sValue);
JOptionPane.showMessageDialog(null, "Slider Value = " + sValue);
}
// ***************inner class change listener.
private class SliderListener implements ChangeListener{
public void stateChanged(ChangeEvent e){
printState(); // wait for a change in state, then print slider value
}
}
}
not only do i not get anything printed, but when i tried to use methods like setSnapToTicks(true) and setMajorTickSpacing(2); nothing happens. I do get a slider in the right spot in my frame, and can move the arrow, but nothing happens. Apparently the listener is not listening. And, why do ticks not appear?
Driving me crazy. Any help greatly appreciated.