Java JSlider setSliderValue(JSlider slider, float f)

Here you can find the source of setSliderValue(JSlider slider, float f)

Description

Set a slider value based on a fraction [0,1] value.

License

Open Source License

Parameter

Parameter Description
slider the slider to adjust.
f a value of [0,1], where 0 will be mapped to the minimum slider value and 1 will be mapped to the maximum slider value.

Declaration

protected static void setSliderValue(JSlider slider, float f) 

Method Source Code

//package com.java2s;
/*//from   ww w  .  j  a v a  2s .  c o  m
 * @(#)BlogHelper.java
 *
 * $Date: 2014-11-27 07:50:51 +0100 (Do, 27 Nov 2014) $
 *
 * Copyright (c) 2014 by Jeremy Wood.
 * All rights reserved.
 *
 * The copyright of this software is owned by Jeremy Wood. 
 * You may not use, copy or modify this software, except in  
 * accordance with the license agreement you entered into with  
 * Jeremy Wood. For details see accompanying license terms.
 * 
 * This software is probably, but not necessarily, discussed here:
 * https://javagraphics.java.net/
 * 
 * That site should also contain the most recent official version
 * of this software.  (See the SVN repository for more details.)
 */

import javax.swing.JSlider;

public class Main {
    /** Set a slider value based on a fraction [0,1] value.
     * 
     * @param slider the slider to adjust.
     * @param f a value of [0,1], where 0 will be mapped to the minimum slider value
     * and 1 will be mapped to the maximum slider value.
     */
    protected static void setSliderValue(JSlider slider, float f) {
        if (f < 0 || f > 1)
            throw new IllegalArgumentException("f (" + f
                    + ") must be between [0, 1]");
        int v = (int) (slider.getMinimum() + f
                * (slider.getMaximum() - slider.getMinimum()));
        slider.setValue(v);
    }
}

Related

  1. makeLabelSliderPanelHorizontal(String text, JSlider slider)
  2. reapplyFontSize(JSlider slider)
  3. setMinMaxSliderLabels(JSlider slider, String[] labels)
  4. setSliderLabels(JSlider slider, int[] values, String[] labels)
  5. setSliderPercent(JSlider s, double percent)