/* @(#)TextFieldSample.java 27 September 2007 */ import edu.neu.ccs.*; import edu.neu.ccs.gui.*; import javax.swing.*; public class TextFieldSample extends DisplayPanel { /** The table panel gap. */ protected int gap = 20; /** The text field view width. */ protected int width = TextFieldView.getSampleWidth("0000000000000000000000"); /** The text field view for x. */ protected TextFieldView xTFV = new TextFieldView(width); /** The label for x. */ protected Annotation xLabel = new Annotation(""); /** The label for the square. */ protected Annotation sLabel = new Annotation(""); /** The label for the square root. */ protected Annotation rLabel = new Annotation(""); /** The label for the inverse. */ protected Annotation iLabel = new Annotation(""); /** The demand computation action. */ protected SimpleAction demandAction = new SimpleAction("Demand Computation") { public void perform() { demand(); } }; /** The request computation action. */ protected SimpleAction requestAction = new SimpleAction("Request Computation") { public void perform() { request(); } }; /** The constructor. */ public TextFieldSample() { createGUI(); } /** Create the GUI. */ protected void createGUI() { // 2-dimension table of labels and text fields Object[][] stuff = { { "x", new Halo(xTFV) }, { "x", xLabel }, { "Square Of x", sLabel }, { "Square Root Of x", rLabel }, { "Inverse Of x", iLabel } }; TablePanel panel = new TablePanel(stuff, gap, gap, WEST); // vertical table with 2-dimensional table and buttons Object[] mainStuff = { panel, demandAction, requestAction }; VTable mainTable = new VTable(mainStuff, gap, gap, CENTER); // decorate mainTable.emptyBorder(gap); mainTable.setDeepBackground(Colors.white); mainTable.setDeepBackground(Colors.yellow, JButton.class); // add the main table to the enclosing GUI addObject(mainTable); // add the request action as the default action xTFV.addActionListener(requestAction); } /** The method to demand x for the computations. */ protected void demand() { double x = xTFV.demandDouble(); xLabel.setText("" + x); sLabel.setText("" + (x * x)); rLabel.setText("" + (Math.sqrt(x))); iLabel.setText("" + (1 / x)); } /** The method to request x for the computations. */ protected void request() { try { double x = xTFV.requestDouble(); xLabel.setText("" + x); sLabel.setText("" + (x * x)); rLabel.setText("" + (Math.sqrt(x))); iLabel.setText("" + (1 / x)); } catch (CancelledException ex) { xTFV.setText(""); xLabel.setText("Cancelled"); sLabel.setText("Cancelled"); rLabel.setText("Cancelled"); iLabel.setText("Cancelled"); } } /** The main method. */ public static void main(String[] args) { new TextFieldSample().frame("Text Field Sample"); } }