Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying Value Label in JSlider

 
Ajay Singh
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

i have made a simple JSlider program with minumum range=0;maximum range=100,and value=35( passed as JSlider slider = new JSlider(0,100,35)).MajorTickSpacing i have set as 10.But i see the knob only points to the supposed 35 location without the label.I want the value label also to be displayed like (0,10,20,30,35,40.....100).Is this possible?
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you set setPaintLabels() to true?
 
Ajay Singh
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Yes its set to true.I think you did not get what i am saying.The labels are displayed 0...10..20..30..............100.But i also want the label based on the value set(35 in this case).
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ajay Singh:
Hi

Yes its set to true.I think you did not get what i am saying.The labels are displayed 0...10..20..30..............100.But i also want the label based on the value set(35 in this case).


You mean to say, you want labels which are incremental of 1 instead of 10?
Like 30,31,32...40?
 
Ajay Singh
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.Here is my code

public class SwingSliderExample1
{
public static void main(String args[])
{
JFrame f = new JFrame("JSlider Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
JSlider slider = new JSlider(0,100,35); //line no 8


slider.setMinorTickSpacing(1);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);

content.add(slider, BorderLayout.CENTER);
f.setSize(800, 500);
f.setVisible(true);
}
}


Here the value labels are displayed 0..10..20......100.i.e in interval of 10.Now in addtion to these labels ,i want 35(which is set in line 35) also to be displayed.So my final range should be 0..10..20..30..35..40..50..60..70..80..90..100
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out JSlider#setLabelTable()
You can customize the label table as per your requirement.
 
Ajay Singh
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks maneesh, i got it now:-)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic