File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Extending JSlider

Craig Jennings
Greenhorn

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.setName(name);
slider.setToolTipText(hover);
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(5);
slider.setPaintTicks(true);
slider.setPaintLabels(true);

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

}
}
});

this.add(slider);
this.setVisible(true);
}

}


----

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

sliders.add(tone);
sliders.add(pitch);
sliders.add(rhythm);
sliders.add(technique);
sliders.add(interpretation);
sliders.setVisible(true);



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: 177
    
    6

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 controls...it 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
Greenhorn

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!

 
wood burning stoves
 
subject: Extending JSlider