On a normal JSlider
the value range displayed left-to-right on a horizontal JSlider
and bottom-to-top on vertical JSlider
. To reverse the slider values from their normal order you can use the setInverted()
method of the JSlider
instance. Passing a true
boolean value into this method call reverse the values order.
package org.kodejava.swing;
import javax.swing.*;
import java.awt.*;
public class JSliderInvertedDemo extends JPanel {
public JSliderInvertedDemo() {
initializeUI();
}
public static void showFrame() {
JPanel panel = new JSliderInvertedDemo();
JFrame frame = new JFrame("Inverted JSlider");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(JSliderInvertedDemo::showFrame);
}
private void initializeUI() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(500, 200));
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 20, 10);
slider.setMinorTickSpacing(1);
slider.setMajorTickSpacing(5);
slider.setPaintLabels(true);
slider.setPaintTicks(true);
// Reverse the value-range of a JSlider. On a normal
// horizontal JSlider the maximum value is on the right
// side. Specifying inverted to true makes the maximum
// value placed on the left side.
slider.setInverted(true);
add(slider, BorderLayout.CENTER);
}
}
The result of the code snippet above is:
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024