// Valuator.java // A class that provides a combination of a scroll bar, text field, // label, and listeners for these. // S. Tanimoto, 1 Feb. 2000 import java.awt.*; import java.awt.event.*; import java.util.*; public class Valuator extends Panel { Scrollbar sb; TextField tf; int theValue; Valuator(String name, int initialValue, int maxVal, Color bgColor) { setBackground(bgColor); sb = new Scrollbar(Scrollbar.VERTICAL, 0, 10, 0, maxVal); sb.setSize(10,300); sb.addAdjustmentListener(new SBHandler()); tf = new TextField(5); tf.addActionListener(new TFHandler()); GridBagLayout gbl = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gbl); gbc.gridx = 1; gbc.gridy = 1; gbc.gridwidth = 2; Label l1 = new Label(name); gbl.setConstraints(l1, gbc); add(l1); gbc.gridx = 1; gbc.gridy = 2; gbc.gridwidth = 1; gbl.setConstraints(sb, gbc); add(sb); gbc.gridx = 2; gbc.gridy = 2; gbc.gridwidth = 1; gbl.setConstraints(tf, gbc); add(tf); theValue = initialValue; tf.setText(theValue + ""); } public int getValue() { return theValue; } class SBHandler implements AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent e) { theValue = e.getValue(); tf.setText(theValue + ""); } } class TFHandler implements ActionListener { public void actionPerformed(ActionEvent e) { theValue = (new Integer(tf.getText())).intValue(); sb.setValue(theValue); } } }