Question

I'm having a annoying issue when calling JSlider#setLabelTable with descriptive labels. The problem is that the track (that usually goes from side to side using all of the sliders width) gets narrowed because of the length of the labels.

By default the slider looks like:

##############################################
|    |    |    |    |    |    |    |    |    |

But with custom labels it looks something like this:

     ############################
     |  |  |  |  |  |  |  |  |  |
Low priority     High priority (Quick delivery)

But I want something like this:

##############################################
|    |    |    |    |    |    |    |    |    |
Low priority                                 High priority (Quick delivery)

Anyone know if this is possible? I'm afraid this involves BasicSliderUI which unfortunately isn't an option since the application supports multiple L&Fs.

Here's an SSCCE showing my problem:

import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.util.Dictionary;
import java.util.Hashtable;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;

public class JSliderLabelTableProblem
{
    public JSliderLabelTableProblem()
    {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setBounds(new Rectangle(200, 200, 400, 200));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createNormalSlider(), BorderLayout.NORTH);
        frame.add(createSliderWithLabels(), BorderLayout.SOUTH);
        frame.setVisible(true);
    }

    private JSlider createNormalSlider()
    {
        JSlider slider = new JSlider();
        slider.setMinimum(1);
        slider.setMaximum(10);
        slider.setMajorTickSpacing(1);
        slider.setSnapToTicks(true);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        return slider;
    }

    private JSlider createSliderWithLabels()
    {
        JSlider slider = createNormalSlider();
        Dictionary<Integer, JComponent> labels = new Hashtable<Integer, JComponent>();
        labels.put(1, new JLabel("Low priority"));
        labels.put(10, new JLabel("High priority (Quick delivery)"));
        slider.setLabelTable(labels);
        return slider;
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new JSliderLabelTableProblem();
            }
        });
    }
}
Was it helpful?

Solution

As you can Use HTML in Swing Components, you might try wrapping the labels to fit better.

labels.put(1, new JLabel("<html>Low<br>priority</html>"));
labels.put(10, new JLabel("<html>High priority<br>(Quick delivery)</html>"));

Addendum: You also might try altering the label's horizontal alignment.

Addendum: Another approach might be to use short labels on the slider itself and add an explanatory legend nearby. If you have to implelent a custom component, I found Kirill Grouchnikov's How to Write a Custom Swing Component helpful.

OTHER TIPS

I think you will have to make your own version of JSlider and override method
public Hashtable createStandardLabels( int increment, int start )
where specially this line is ready for changes
put( new Integer ( labelIndex ), new LabelUIResource( ""+labelIndex, JLabel.CENTER ) );

according to code found at:
http://kickjava.com/src/javax/swing/JSlider.java.htm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top