package com.mindprod.example;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.DecimalFormat;
import java.util.Hashtable;
import static java.lang.System.*;
/**
* demonstrate the use of a calibrated javax.swing.JSlider.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.1 2014-06-03 notes about Nimbus and opaque
* @since 2014-06-03
*/
public final class TestJSlider
{
/**
* used to calibrate marginWidthSlider
*/
private static final DecimalFormat DF1 = new DecimalFormat( "0.0" );
/**
* Debugging harness for a Frame
*
* @param args command line arguments are ignored.
*/
public static void main( String args[] )
{
SwingUtilities.invokeLater( new Runnable()
{
/**
* fire up a JFrame on the Swing thread
*/
public void run()
{
try
{
UIManager.setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" );
}
catch ( Exception e )
{
err.println( "Can't install Nimbus L&F" );
}
final JFrame jFrame = new JFrame();
final Container contentPane = jFrame.getContentPane();
contentPane.setBackground( new Color( 0xf7fbd7 ) );
final JSlider marginWidthSlider = new JSlider( JSlider.HORIZONTAL, 0 , 20 * 2 , 3 * 2 );
marginWidthSlider.setMajorTickSpacing( 20 );
marginWidthSlider.setMinorTickSpacing( 2 );
marginWidthSlider.setPaintTicks( true );
@SuppressWarnings( "unchecked" ) Hashtable<Integer, JLabel> sliderLabels = marginWidthSlider.createStandardLabels( 10, 0 );
for ( int i = 0; i <= 20 * 2; i += 20 / 2 )
{
sliderLabels.get( i ).setText( DF1.format( ( double ) i / 20 ) );
}
marginWidthSlider.setLabelTable( sliderLabels );
marginWidthSlider.setPaintLabels( true );
marginWidthSlider.setPaintTrack( true );
marginWidthSlider.addChangeListener( new ChangeListener()
{
@Override
public void stateChanged( final ChangeEvent e )
{
final int marginInTwentieths = marginWidthSlider.getValue();
out.println( marginInTwentieths );
}
}
);
contentPane.setLayout( new GridBagLayout() );
contentPane.add( marginWidthSlider,
new GridBagConstraints( 0,
0,
1,
1,
1.0,
1.0,
GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL,
new Insets( 20, 20, 20, 20 ),
0,
0 ) );
jFrame.setSize( 400, 150 );
jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
jFrame.validate();
jFrame.setVisible( true );
}
} );
}
}