The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JSlider  [7 examples]

e795. Getting and Setting the Values of a JSlider Component

    // To create a slider, see e794 Creating a JSlider Component
    
    // Get the current value
    int value = slider.getValue();
    
    // Get the minimum value
    int min = slider.getMinimum();
    
    // Get the maximum value
    int max = slider.getMaximum();
    
    // Get the extent
    int extent = slider.getExtent();
    // Change the minimum value
    int newMin = 0;
    slider.setMinimum(newMin);
    
    // Change the maximum value
    int newMax = 256;
    slider.setMaximum(newMax);
    
    // Set the value; the new value will be forced into the bar's range
    int newValue = 33;
    slider.setValue(newValue);
    
    // Set the extent
    int newExtent = 10;
    slider.setExtent(newExtent);
It is also possible to set all the values at once by using the model:
    slider.getModel().setRangeProperties(newValue, newExtent, newMin, newMax, false);

 Related Examples
e794. Creating a JSlider Component
e796. Setting the Orientation of a JSlider Component
e797. Showing Tick Marks on a JSlider Component
e798. Showing Tick-Mark Labels on a JSlider Component
e799. Constraining JSlider Component Values to Tick Marks
e800. Listening for Value Changes in a JSlider Component

See also: Actions    JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JList    JProgressBar    JRadioButton    JScrollPane    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.