import java.awt.*; import java.awt.event.*; import java.awt.TextField.*; import java.awt.Scrollbar.*; public class TextSlide extends Panel { TextField textField; Scrollbar slider; bender owner; double theVal; int scrollmin = 0, scrollmax = 1000; double minVal = 0.0, maxVal = 5.0; TextSlide(bender controller, String myLabel, double min, double max) { owner = controller; minVal = min; maxVal = max; System.out.println("slider min: " + minVal + "\n" + "slider max: " + maxVal); this.setBackground(Color.white); GridBagConstraints gbc = new GridBagConstraints(); GridBagLayout gbl = new GridBagLayout(); setLayout(gbl); gbc.anchor = GridBagConstraints.WEST; gbc.gridx = 0; gbc.gridy = 0; Label title = new Label(myLabel); add(title); gbl.setConstraints(title,gbc); gbc.anchor = GridBagConstraints.NORTH; gbc.gridy = 1; textField = new TextField("0.6",10); add(textField); textField.addActionListener(new textListener(this)); gbl.setConstraints(textField,gbc); // gbc.anchor = GridBagConstraints.CENTER; gbc.gridy = 2; gbc.gridheight = GridBagConstraints.REMAINDER; gbc.fill = GridBagConstraints.VERTICAL; gbc.weighty = 2; slider = new Scrollbar(Scrollbar.VERTICAL,1,100,scrollmin,scrollmax); add(slider); slider.addAdjustmentListener(new sliderListener(this)); gbl.setConstraints(slider,gbc); setValue(0.6); validate(); } class textListener implements ActionListener { TextSlide parent; public textListener(TextSlide mother) { parent = mother; } public void actionPerformed(ActionEvent e) { if (e.getSource() == textField) { double f = getTextValue(); if ((f>=minVal) && (f<=maxVal)) { theVal = f; setValue(f); owner.changeParam(parent); } } } } class sliderListener implements AdjustmentListener { TextSlide parent; public sliderListener(TextSlide mother) { parent = mother; } public void adjustmentValueChanged(AdjustmentEvent e) { if (e.getSource() == slider) { double sv = getSliderValue(); if ((sv>=minVal) && (sv<=maxVal)) { theVal = sv; setValue(sv); owner.changeParam(parent); } } } } // Set the slider and text fields to the same floating point number. public void setValue(double f) { if (fmaxVal) { f = maxVal; } theVal = f; setTextValue(f); setSliderValue(f); } public double getValue() { return theVal; } // Calculate what slider position corresponds to a number and set the position. private void setSliderValue(double f) { slider.setValue( (int)(scrollmin+ (f-minVal)/(maxVal-minVal)*(scrollmax-scrollmin))); } // A local function to find the number typed in the textbox. private double getTextValue() { double f; // Catch possible non-numbers. try { f = Double.valueOf(textField.getText()).doubleValue(); } catch (java.lang.NumberFormatException e) { f = minVal-1.0; } // Don't let them kill the animation with crazy numbers. if (f>maxVal) { f = maxVal; setTextValue(f); } else if (f