/Users/lyon/j4p/src/gui/run/RunSlider.java

1    package gui.run; 
2     
3    import javax.swing.*; 
4    import javax.swing.event.ChangeListener; 
5    import javax.swing.event.ChangeEvent; 
6    import java.awt.*; 
7    import java.util.Enumeration; 
8    import java.util.Dictionary; 
9     
10    
11   public abstract class RunSlider 
12           extends JSlider implements 
13           ChangeListener, Runnable { 
14    
15       private JLabel valueLabel = new JLabel(getValue() + ""); 
16       private String identifier = ""; 
17    
18    
19       public RunSlider( 
20               int orientation, 
21               int min, int max, int defaultValue) { 
22           super(orientation, min, max, defaultValue); 
23           setPaintTicks(true); 
24           int majorTickSpacking = (max - min) / 4; 
25           setMajorTickSpacing(majorTickSpacking); 
26           setMinorTickSpacing(majorTickSpacking / 5); 
27           setPaintLabels(true); 
28           addChangeListener(this); 
29       } 
30       /** 
31        * Set all the labels to the same font. 
32        * @param f 
33        */ 
34       public void setTickLabelFonts(Font f) { 
35           Dictionary d = getLabelTable(); 
36           Enumeration e = d.elements(); 
37           while (e.hasMoreElements()) { 
38               Object o = e.nextElement(); 
39               if (o instanceof JLabel) { 
40                   JLabel jl = (JLabel) o; 
41                   jl.setFont(f); 
42               } 
43           } 
44       } 
45    
46       public RunSlider() { 
47           this(javax.swing.JSlider.HORIZONTAL, 0, 100, 50); 
48       } 
49        /** 
50        * Creates a slider using the specified orientation with the 
51        * range 0 to 100 and an initial value of 50. 
52        */ 
53       public RunSlider(int orientation) { 
54           this(orientation, 0, 100, 50); 
55       } 
56        /** 
57        * Creates a horizontal slider using the specified min and max 
58        * with an initial value equal to the average of the min plus max. 
59        */ 
60       public RunSlider(int min, int max) { 
61           this(javax.swing.JSlider.HORIZONTAL, min, max, (max + min) / 2); 
62       } 
63        /** 
64        * Creates a horizontal slider using the specified min, max and value. 
65        */ 
66       public RunSlider(int min, int max, double defaultValue) { 
67           this(javax.swing.JSlider.HORIZONTAL, min, max, (int) defaultValue); 
68       } 
69       /** 
70        * Creates a horizontal slider using the specified min, max and value. 
71        */ 
72       public RunSlider(int min, int max, double defaultValue, int orientation) { 
73           this(orientation, min, max, (int) defaultValue); 
74       } 
75    
76       public RunSlider(double min, double max, double defaultValue) { 
77           this(javax.swing.JSlider.HORIZONTAL, 
78                   (int) (min * 100), (int) (max * 100), 
79                   (int) (defaultValue * 100)); 
80       } 
81    
82    
83       public void stateChanged(ChangeEvent ae) { 
84           valueLabel.setText(identifier + getValue() + ""); 
85           SwingUtilities.invokeLater(this); 
86       } 
87    
88    
89       public JLabel getValueLabel() { 
90           return valueLabel; 
91       } 
92    
93       public void setValueLabel(JLabel vl) { 
94           valueLabel = vl; 
95       } 
96    
97       public void setIdentifier(String id) { 
98           identifier = id; 
99       } 
100   
101   
102      public static RunSlider getLabeledSlider(String s, RunSlider rs) { 
103          s = s + "="; 
104          JLabel valueLabel = new JLabel(s + rs.getValue()); 
105          rs.setValueLabel(valueLabel); 
106          rs.setIdentifier(s); 
107          return rs; 
108      } 
109   
110      public static void main(String args[]) { 
111          gui.ClosableJFrame cf = new gui.ClosableJFrame(); 
112          cf.setSize(200, 200); 
113          java.awt.Container c = cf.getContentPane(); 
114          c.setLayout(new java.awt.BorderLayout()); 
115   
116          c.add(new PlusMinusSlider(), BorderLayout.NORTH); 
117          c.add(new RunSlider(RunSlider.HORIZONTAL) { 
118              public void run() { 
119                  System.out.println( 
120                          "value=" + getValue()); 
121                  this.setTickLabelFonts(new Font("times",Font.BOLD,getValue())); 
122              } 
123          }, BorderLayout.SOUTH); 
124          c.add(new RunSlider(RunSlider.VERTICAL) { 
125              public void run() { 
126                  System.out.println( 
127                          "value=" + getValue()); 
128              } 
129          }, BorderLayout.EAST); 
130          c.add(new RunSlider(RunSlider.VERTICAL) { 
131              public void run() { 
132                  System.out.println( 
133                          "value=" + getValue()); 
134              } 
135          }, BorderLayout.WEST); 
136          RunSlider centerSlider = new RunSlider(RunSlider.VERTICAL) { 
137                      public void run() { 
138                          System.out.println( 
139                                  "value=" + getValue()); 
140                      } 
141                  }; 
142          centerSlider.setToolTipText( 
143                  "dont play in the highway without a car"); 
144          c.add(centerSlider, BorderLayout.CENTER); 
145          cf.setVisible(true); 
146      } 
147   
148      private static class PlusMinusSlider extends RunSlider { 
149          public PlusMinusSlider() { 
150              super(RunSlider.HORIZONTAL, -10, 10, -5); 
151          } 
152   
153          public void run() { 
154              System.out.println( 
155                      "value=" + getValue()); 
156          } 
157      } 
158  }