001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 package graphlab.ui.components.utils; 005 006 import graphlab.platform.lang.BoundedInteger; 007 008 import javax.swing.*; 009 import javax.swing.event.ChangeEvent; 010 import javax.swing.event.ChangeListener; 011 012 /** 013 * @author azin azadi 014 */ 015 public class LabledSlider extends JComponent implements ChangeListener { 016 /** 017 * 018 */ 019 private static final long serialVersionUID = -8976402299284319256L; 020 JSlider slider; 021 private JLabel label; 022 023 public JSlider getSlider() { 024 return slider; 025 } 026 027 public LabledSlider(BoundedInteger value) { 028 slider = new JSlider(JSlider.HORIZONTAL, value.getMin(), value.getMax(), value.getValue()); 029 slider.setOpaque(false); 030 label = new JLabel(); 031 updateLabel(); 032 slider.addChangeListener(this); 033 initComponents(); 034 validate(); 035 } 036 037 private void initComponents() { 038 java.awt.GridBagConstraints gridBagConstraints; 039 040 setLayout(new java.awt.GridBagLayout()); 041 042 label.setLabelFor(slider); 043 add(label, new java.awt.GridBagConstraints()); 044 045 gridBagConstraints = new java.awt.GridBagConstraints(); 046 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 047 gridBagConstraints.weightx = 1.0; 048 add(slider, gridBagConstraints); 049 } 050 051 052 public void stateChanged(ChangeEvent e) { 053 updateLabel(); 054 } 055 056 private void updateLabel() { 057 label.setText(slider.getValue() + ""); 058 } 059 }