aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes simple slider question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "simple slider question" Watch "simple slider question" New topic

simple slider question

Steven Coddington

Joined: Oct 17, 2004
Posts: 10
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();

Container c = myWindow.getContentPane();

SliderPanel s = new SliderPanel();
c.add(s, BorderLayout.SOUTH);


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

// ***************** method to display output from slider
public void printState(){
sValue = s.getValue();
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.

Steve Coddington
Pavan Kumar Srinivasan

Joined: Sep 17, 2008
Posts: 27
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

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

Joined: Oct 27, 2005
Posts: 19693

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 ]

How To Ask Questions How To Answer Questions
Steven Coddington

Joined: Oct 17, 2004
Posts: 10
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

Joined: May 03, 2008
Posts: 4542

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 ]

luck, db
There are no new questions, but there may be new answers.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19693

Originally posted by Steven Coddington:

Change that to:

This will call the JSlider(int, int, int) constructor but for the current slider object.
I agree. Here's the link:
subject: simple slider question