Here you can find the source of createSliderPanel(final JSlider slider, String label, int width)
Parameter | Description |
---|---|
slider | the slider |
label | the label |
width | the width |
public static JPanel createSliderPanel(final JSlider slider, String label, int width)
//package com.java2s; /*/*from w ww.j a v a 2 s . c om*/ * Copyright 1997-2013 Fabien Michel, Olivier Gutknecht, Jacques Ferber * * This file is part of MaDKit. * * MaDKit is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * MaDKit is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with MaDKit. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.Dimension; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import javax.swing.BoundedRangeModel; import javax.swing.BoxLayout; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.border.TitledBorder; public class Main { /** * Creates a labeled panel containing a slider with default size. * * @param slider the slider * @param label the label * @return a panel for the slider */ public static JPanel createSliderPanel(final JSlider slider, String label) { return createSliderPanel(slider, label, 170); } /** * Creates a labeled panel containing a slider and considering a particular * width * * @param slider the slider * @param label the label * @param width the width * @return a panel for the slider */ public static JPanel createSliderPanel(final JSlider slider, String label, int width) { final JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); p.setBorder(new TitledBorder(label)); p.setPreferredSize(new Dimension(width, 60)); p.add(slider); return p; } /** * Creates a labeled panel containing a slider built using an existing * {@link DefaultBoundedRangeModel} * * @param model the model * @param label the label * @return a panel for this model */ public static JPanel createSliderPanel(final BoundedRangeModel model, String label) { return createSliderPanel(createJSlider(model), label); } /** * Creates a JSlider built using a {@link DefaultBoundedRangeModel} and * containing a {@link MouseWheelListener} and some usual default settings * * @param model the model * @return the corresponding {@link JSlider} */ public static JSlider createJSlider(final BoundedRangeModel model) { final JSlider slider = new JSlider(model); slider.addMouseWheelListener(new MouseWheelListener() { @Override public void mouseWheelMoved(MouseWheelEvent e) { slider.setValue(-e.getWheelRotation() * model.getMaximum() / 100 + model.getValue()); } }); slider.setPaintTicks(true); slider.setPaintLabels(true); // slider.setMajorTickSpacing(model.getMaximum()/2); // slider.setMinorTickSpacing(model.getMaximum()/10); // slider.setSnapToTicks(true); return slider; } }