• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple slider question

 
Steven Coddington
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pavan Kumar Srinivasan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1: The instance of jslider being added to the parent Frame is that of the SliderPanel , but the listeners are being added to a different instance of jslider.

2: To create labels for values use

setPaintLabels(true);
setLabelTable(createStandardLabels('some increment value' ) );

[ October 19, 2008: Message edited by: Pavan Kumar Srinivasan ]
[ October 19, 2008: Message edited by: Pavan Kumar Srinivasan ]
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pavan is right - you have a JSlider subclass with a JSlider as instance field.

Remove that JSlider s and replace all references to it with "this" (or omit all occurrences of s.).

Oh, and please Use Code Tags.
[ October 20, 2008: Message edited by: Rob Prime ]
 
Steven Coddington
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help. slider is now working, but 'setSnapToTicks(true)' is not. I can slide to anything in between. What am I doing wrong here?

 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You still have a class that extends JSlider with an instance field of type JSlider and construct a new JSlider in the method SliderPanel() which you do not retain a reference to.

Please go through some tutorials, starting here:
The Java� Tutorials
[ October 20, 2008: Message edited by: Darryl Burke ]
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steven Coddington:

Change that to:

This will call the JSlider(int, int, int) constructor but for the current slider object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic