To create a vertical JSlider
set the orientation on the JSlider
‘s constructor to JSlider.VERTICAL
. If you do not pass JSlider.VERTICAL
as a constructor parameter use the setOrientation()
method instead.
package org.kodejava.swing;
import javax.swing.*;
import java.awt.*;
public class JSliderVertical extends JPanel {
public JSliderVertical() {
initializeUI();
}
public static void showFrame() {
JPanel panel = new JSliderVertical();
panel.setOpaque(true);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle("Vertical JSlider");
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(JSliderVertical::showFrame);
}
private void initializeUI() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(500, 200));
// Creates a vertical JSlider that accept value in the
// range between 0 and 20. The initial value is set to 4.
JSlider slider = new JSlider(JSlider.VERTICAL, 0, 20, 4);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setMinorTickSpacing(1);
slider.setMajorTickSpacing(4);
add(slider, BorderLayout.CENTER);
}
}
The screen capture of the code snippet above.
Latest posts by Wayan (see all)
- How do I add an object to the beginning of Stream? - February 7, 2025
- 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