Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
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
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
JSlider with decimal point increments
Jody Monahan
Greenhorn
Posts: 20
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi all,
I was just wondering if it is possible to set a JSlider with a minimum of '0' and a max of '1' with increments of lets say '0.1'?
I have obviously tried the following code, however it displays only one label - '0' and only one tick, also at '0'.
Any help would be greatly appreciated.
JSlider slider = new JSlider (0, 1); slider.setMinorTickSpacing (0.1); slider.setMajorTickSpacing (0.5); slider.setPaintTicks (true); slider.setPaintLabels (true);
Michael Dunn
Ranch Hand
Posts: 4632
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
not sure if there's any new inbuilt way to do it, but here's the old way
import javax.swing.*; import java.awt.*; import javax.swing.event.*; class Testing { public void buildGUI() { final JSlider slider = new JSlider(0, 100, 1); slider.setMajorTickSpacing(25); slider.setPaintTicks(true); java.util.Hashtable<Integer,JLabel> labelTable = new java.util.Hashtable<Integer,JLabel>(); labelTable.put(new Integer(100), new JLabel("1.0")); labelTable.put(new Integer(75), new JLabel("0.75")); labelTable.put(new Integer(50), new JLabel("0.50")); labelTable.put(new Integer(25), new JLabel("0.25")); labelTable.put(new Integer(0), new JLabel("0.0")); slider.setLabelTable( labelTable ); slider.setPaintLabels(true); JFrame f = new JFrame(); f.getContentPane().add(slider); f.pack(); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); slider.addChangeListener(new javax.swing.event.ChangeListener(){ public void stateChanged(javax.swing.event.ChangeEvent ce){ System.out.println(slider.getValue()/100.0); } }); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run(){ new Testing().buildGUI(); } }); } }
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
what accounts for precision problems when computing floating point numbers?
How to display labels on a JSlider
background bleed-through when using getInsets.
Array value
JSlider value
More...