JSlider rendering with custom LabelTable
Asked Answered
B

1

5

I want to use a JSlider to browse some chronological events, so I set a custom LabelTable so that it displays some Dates instead of the default integer values. My code goes like this:

JSlider slider = new JSlider();
...
Date[] dates = getDates();
        slider.setModel(new DefaultBoundedRangeModel(0, 0, 0, dates.length - 1));
        Hashtable<Integer, JLabel> ht = new Hashtable<Integer, JLabel>();
        for (int i = 0; i < dates.length; ++i) {
            JLabel label = new JLabel(DateFormat.getDateInstance().format(dates[i]));
            ht.put(i, label);
        }
        slider.setLabelTable(ht);
        slider.setPaintLabels(true);
        slider.setInverted(true);

This works fine, as you can see: enter image description here

However, if I want to change the orientation of the slider, say slider.setOrientation(JSlider.HORIZONTAL);, here is the result: enter image description here

What if I want to, say, display one date above/one date below the slider on every other date? Also, can I display the labels to the left of the slider on the Vertical slider?

Regards, Remi

Bolding answered 11/2, 2013 at 8:37 Comment(1)
I'd be layed that correctly, please post and SSCCE, short, runnable, compilable, just about JFrame, with JSlider and with hardcoded value for JSliderMix
E
7

Changing the labels side (simply apply RTL orientation):

public static void main ( String[] args )
{
    try
    {
        UIManager.setLookAndFeel ( new NimbusLookAndFeel () );
    }
    catch ( UnsupportedLookAndFeelException e )
    {
        e.printStackTrace ();
    }


    JFrame frame = new JFrame ();

    JSlider slider = new JSlider ( SwingConstants.VERTICAL );
    slider.setPaintLabels ( true );
    slider.setComponentOrientation ( ComponentOrientation.RIGHT_TO_LEFT );

    Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel> ();
    table.put ( 0, new JLabel ( "May 2, 2000" ) );
    table.put ( 25, new JLabel ( "May 3, 2001" ) );
    table.put ( 50, new JLabel ( "May 4, 2002" ) );
    table.put ( 75, new JLabel ( "May 5, 2003" ) );
    table.put ( 100, new JLabel ( "May 6, 2004" ) );
    slider.setLabelTable ( table );

    frame.add ( slider );

    frame.pack ();
    frame.setLocationRelativeTo ( null );
    frame.setVisible ( true );
}

Result:

enter image description here

To vary labels placement you will have to modify them manually. This can be done for example this way:

public static void main ( String[] args )
{
    try
    {
        UIManager.setLookAndFeel ( new NimbusLookAndFeel () );
    }
    catch ( UnsupportedLookAndFeelException e )
    {
        e.printStackTrace ();
    }


    JFrame frame = new JFrame ();
    frame.getRootPane ().setBorder ( BorderFactory.createEmptyBorder ( 5, 5, 5, 5 ) );

    JSlider slider = new JSlider ( SwingConstants.HORIZONTAL );
    slider.setPaintLabels ( true );

    Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel> ();
    table.put ( 0, new JLabel ( "May 2, 2000" ) );
    JLabel l2 = new JLabel ( "May 3, 2001" );
    l2.setBorder ( BorderFactory.createEmptyBorder ( 20, 0, 0, 0 ) );
    table.put ( 25, l2 );
    table.put ( 50, new JLabel ( "May 4, 2002" ) );
    JLabel l3 = new JLabel ( "May 5, 2003" );
    l3.setBorder ( BorderFactory.createEmptyBorder ( 20, 0, 0, 0 ) );
    table.put ( 75, l3 );
    table.put ( 100, new JLabel ( "May 6, 2004" ) );
    slider.setLabelTable ( table );

    frame.add ( slider );

    frame.pack ();
    frame.setLocationRelativeTo ( null );
    frame.setVisible ( true );
}

Result:

enter image description here

Ofcourse you could improve that code and add the border according to previous label preferred height (taken from preferred size) in your labels creation cycle each even run.

Erichericha answered 11/2, 2013 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.