Question

I'm trying to use JSlider for the first time and I tried consulting How to Use Sliders to learn the basics. But when I try to make the ticks appear like this:

 sliderInterior = new JSlider(JSlider.HORIZONTAL,-100,100,0);
 sliderInterior.setMinorTickSpacing(5);
 sliderInterior.setMajorTickSpacing(10);
 sliderInterior.setPaintTicks(true);

The ticks just don't appear. What's the problem?

EDIT Did a SSCCE as requested. The problem remains, a JSlider with no ticks at all.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import javax.swing.JOptionPane;
import javax.swing.JSlider;

    public class JSliderTicks extends javax.swing.JDialog {

    public JSliderTicks(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        slider = new JSlider(JSlider.HORIZONTAL,0,10,5);
        slider.setMinorTickSpacing(1);
        slider.setMajorTickSpacing(2);
        slider.setPaintTicks(true);

        JOptionPane.showMessageDialog(null, slider);

        setVisible(true);
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        slider = new javax.swing.JSlider();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addComponent(slider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(115, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(117, 117, 117)
                .addComponent(slider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(160, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JSliderTicks.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JSliderTicks.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JSliderTicks.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JSliderTicks.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JSliderTicks dialog = new JSliderTicks(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JSlider slider;
    // End of variables declaration                   
}

I only changed the constructor since I'm using JDialog Form and the rest is implemented automatically.

I just noticed, if I use JOptionPane the JSlider works fine but in the JDialog it stays unaltered.

Was it helpful?

Solution 2

What is happening is that you're creating an new JSlider in the consructor, when one is already create in the initComponents and added. Also, the JOptionPane, messes it up also, as a component can only be added to one parent container. I removed the JOptionPane and re-initialization of the JSlider and it works. I also set the minimum, maximum, and value accordingly.

    initComponents();

    //slider = new JSlider(JSlider.HORIZONTAL,0,10,5);
    slider.setMinimum(0);
    slider.setMaximum(10);
    slider.setValue(5);
    slider.setMinorTickSpacing(1);
    slider.setMajorTickSpacing(2);
    slider.setPaintTicks(true);

    //JOptionPane.showMessageDialog(null, slider);

    //setVisible(true);                              <<---- already set visible 

OTHER TIPS

It works just fine in this SSCCE.

Slider Ticks

import java.awt.*;
import javax.swing.*;

class SliderTicks {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JSlider sliderInterior = new JSlider(JSlider.HORIZONTAL, -100, 100, 0);
                sliderInterior.setMinorTickSpacing(5);
                sliderInterior.setMajorTickSpacing(10);
                sliderInterior.setPaintTicks(true);

                JOptionPane.showMessageDialog(null, sliderInterior);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top