import java.awt.*; import java.awt.event.*; import java.util.*; /** (C) 2000 Charles Consulting, LLC
AWT panel which contains integer text field and increment/decrement buttons */ public class IntegerPanel extends Panel { private int value; private int min; private int max; private TextField tfield; private Label label; private ArrowButton upButton; private ArrowButton downButton; private ActionListener theListener; private ButtonPanel buttonPanel; /** constructor; label to apply ,initial value, mininum value, maxium value */ public IntegerPanel(String lab,int initialValue,int min_, int max_) { super( ); min = min_; max = max_; label = new Label(lab); value = initialValue; tfield = new TextField(Integer.toString(value),3); upButton = new ArrowButton(true); downButton = new ArrowButton(false); buttonPanel = new ButtonPanel( ); setLayout(new BorderLayout( )); add(label,BorderLayout.WEST); add(buttonPanel,BorderLayout.EAST); processChange( ); //set enabled state tfield.addActionListener( new ActionListener () { public void actionPerformed(ActionEvent e) { try { value = Integer.parseInt(tfield.getText( )); } catch (NumberFormatException nfe) {} IntegerPanel.this.processChange( ); } }); upButton.addActionListener( new ActionListener () { public void actionPerformed(ActionEvent e) { ++value; IntegerPanel.this.processChange( ); } }); downButton.addActionListener( new ActionListener () { public void actionPerformed(ActionEvent e) { --value; IntegerPanel.this.processChange( ); } }); } /** add a action Listener. Class only supports one */ public void addActionListener (ActionListener al) { theListener = al; } public void removeActionListener (ActionListener al) { if (theListener == al) theListener = null; } /** return the set value */ public final int getValue( ) { return value; } private void processChange( ) { if (value < min) value = min; if (value > max) value = max; downButton.setEnabled(value > min); upButton.setEnabled(value < max); tfield.setText(Integer.toString(value)); if (theListener!=null) { ActionEvent ev = new ActionEvent(this,ActionEvent.ACTION_PERFORMED,"ValueChanged"); theListener.actionPerformed(ev); } } /** inner class to support layout */ private class ButtonPanel extends Panel { public ButtonPanel( ) { super( ); setLayout( new FlowLayout(FlowLayout.CENTER,0,0) ); add(tfield); add(upButton); add(downButton); } } }