This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Extending JSlider Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Extending JSlider" Watch "Extending JSlider" New topic

Extending JSlider

Craig Jennings

Joined: Jan 17, 2012
Posts: 2
Hi folks -
I'm pretty new to this, so I don't mind if you explain it to me as if I'm four years old ;)

I'm trying to extend (I think that's the right term) JSlider so I can have multiple sliders in the container, each tracking its own value and each having its own name and tooltip.
When I call the class to add to the container, I get a slider, but it ignores the class information (no ToolTipText, no tick spacing, et cetera).

Here's what I have for the class

import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class mySlider extends JSlider{

public mySlider(String name, String hover){
JSlider slider = new JSlider(JSlider.HORIZONTAL, 1, 100, 50);

slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent evt) {
JSlider slider = (JSlider) evt.getSource();
if (!slider.getValueIsAdjusting()) {





And here's the call:

Container sliders = frame.getContentPane();
sliders.setLayout(new GridLayout(5,1));

mySlider tone = new mySlider("Tone", "vowel purity, resonance vitality, focus, blend");
mySlider pitch = new mySlider("Pitch", "pitch, intonation, tuning");
mySlider rhythm = new mySlider("Rhythm", "rhythm, tempo, note and rest values");
mySlider technique = new mySlider("Technique", "diction, pronunciation, attacks, releases, breath, flexibility");
mySlider interpretation = new mySlider("Interpretation", "phrasing, dynamics, expressiveness, appearance, poise, demand");


Like I said, I'm pretty new - I'm more or less feeling my way, so if I've done something backward, please let me know.
Thank you for your time!
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171

One of the things I notice is that you're extending JSlider, so mySlider IS-A JSlider. There's no need to create a JSlider in the constructor (one already exists - the one you're creating). Rather you should call the JSlider constructor using "super( ...JSlider constructor arguments... );" then use the "this" keyword to set the properties directly, so "this.setName(..)" or just setName(..) like any method call.

When you don't make a super() call, Java defaults to a no-argument constructor. What I imagine is happening is the following:
Call new mySlider(..); the mySlider constructor calls super() which instantiates a default JSlider.
You create a new JSlider and manipulate its properties.
You then add a JSlider to the existing JSlider (since mySlider IS-A JSlider) control. So now your JSlider components are present in duplicate.
I don't know how Java would render a JSlider with multiple may just ignore the second slider.

By the way, convention is to capitalize the first letter of any class, so it would be class MySlider rather than class mySlider. There is also a code button for formatting your code rather than displaying inline which makes it easier to read.

Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Craig Jennings

Joined: Jan 17, 2012
Posts: 2
Wow, thank you! That's got it! Now I'll play around with it a bit more (and likely break it) but I'm making progress!
Thank you so much!

GeeCON Prague 2014
subject: Extending JSlider