• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

To make JLabel get the value from JSlider

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have made JLabel and I would like to make it get the value from JSlider. As JSlider changes, the JLabel should change also. How Can I do it?

I wrote but it just gets the initial value of slider and it doesn't change as slider value changes.

What could be the problem here?
 
Marshal
Posts: 76095
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you put any Listeners on the JSlider?
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No I thought about that, but I'm not sure how to write it. Any hint...?
 
Sheriff
Posts: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the methods JSlider has that start with "add" and end with "Listener". For each create an implementation that only prints the event. You should then be able to choose the one you need.

Don't forget that most component classes inherit listener methods from their super classes. But in this case you don't need to check those of JComponent; those defined in JSlider are all you'll need.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been looking and I wrote this:



It works fine, it prints numbers while I move the slider. But when I tried to put it in JLabel, it reported error since it expected string and found integer. I was intending to write toString() but it doesn't recognize it. How should I do this conversion of types?
 
Campbell Ritchie
Marshal
Posts: 76095
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What sort of value are you getting? Integer, integer or int? Look in the Integer class and the String class for methods to convert ints to Strings.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Value is int.

I have succeed to solve the problem.

Here is how it looks:



It was only necessary to add here setting text to label so it would change as slider value changes. Also, label text should be initialized same as slider's position when it starts to run.

Thank you guys for interesting

Greetings for all!
 
Campbell Ritchie
Marshal
Posts: 76095
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic