import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; /** * This is an applet for demonstrating the FormattedInteger class. We present * a graphical interface that allows the user to enter an integer in one of three * formats: signed decimal, hex, and octal. Whichever format the user enters, * the number is displayed in all three formats. This can be run either as an * applet or as an application. * @author Adam Webber */ public class FormattedIntegerDemo extends JApplet { private static final long serialVersionUID = 0L; /** * Our decimal formatted integers. */ private FormattedInteger decimal = new FormattedInteger(FormattedInteger.PLAIN); /** * Our hex formatted integers. */ private FormattedInteger hex = new FormattedInteger(FormattedInteger.HEX); /** * Our octal formatted integers. */ private FormattedInteger octal = new FormattedInteger(FormattedInteger.OCTAL); /** * A label that reports the last error string. * This is kept in sync with lastError by the update method. * @see update */ private JLabel lastErrorLabel; /** * The text field in which new integers are entered in decimal. */ private JTextField decimalField; /** * The text field in which new integers are entered in hex. */ private JTextField hexField; /** * The text field in which new integers are entered in octal. */ private JTextField octalField; /** * Create our GUI components. */ @Override public void init() { // Boilerplate: create components on event-dispatching thread. try { javax.swing.SwingUtilities.invokeAndWait(new Runnable() { public void run() { createGUI(); } }); } catch (Exception e) { System.err.println("createGUI didn't successfully complete"); } } /** * Create the GUI components. This initializes the applet. */ public void createGUI() { // First, create the Labels, and the TextFields where the user will // enter formatted integers. JLabel decimalLabel = new JLabel("Signed decimal integer: ", JLabel.RIGHT); decimalField = new JTextField(20); decimalField.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent evt) { update(decimal,decimalField.getText()); } }); JLabel hexLabel = new JLabel("Hex integer (with leading 0x): ", JLabel.RIGHT); hexField = new JTextField(20); hexField.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent evt) { update(hex,hexField.getText()); } }); JLabel octalLabel = new JLabel("Octal integer (with leading 0): ", JLabel.RIGHT); octalField = new JTextField(20); octalField.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent evt) { update(octal,octalField.getText()); } }); lastErrorLabel = new JLabel("Hello!", JLabel.CENTER); lastErrorLabel.setForeground(Color.RED); // Create an inner Panel for the Label/TextField lines. JPanel top = new JPanel(new GridLayout(3,0)); top.add(decimalLabel); top.add(decimalField); top.add(hexLabel); top.add(hexField); top.add(octalLabel); top.add(octalField); // Finally, our content Panel: we will use a BorderLayout with the Label/TextField // lines in the north border, and the error Label in the south. Container content = getContentPane(); content.add(top, BorderLayout.NORTH); content.add(lastErrorLabel, BorderLayout.SOUTH); // Get a consistent initial state for the labels and text fields. update(decimal,"0"); lastErrorLabel.setText("Enter a number in any field"); // Initial greeting } /** * A main method, so we can be run as an application, as well as a JApplet. */ public static void main(String[] args) { final JApplet applet = new FormattedIntegerDemo(); applet.init(); final JFrame frame = new JFrame("Formatted Integer Tester"); frame.setContentPane(applet.getContentPane()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } /** * Update our display. We are given one of the FormattedIntegers, and a * String the users wants entered into it. We try to enter the user's * String. If there is an error, this will produce an error message, and * no change to the number stored in the FormattedInteger. We then update * all the FormattedIntegers to the same number, and update our display of * the text for each, and our display of the error text. * @param form the FormattedInteger to enter a string into * @param number the String to enter */ private void update(FormattedInteger form, String number) { // Try to set the formatted integer to the given string. We get an error // string back, which will be the empty string if there is no error. We // display the string. String lastError = form.setString(number); lastErrorLabel.setText(lastError); // Update all the formatted integers to the same integer value now stored // in form. (This will not have changed if there was an error.) Update // our display to match the formatted strings. int val = form.getInt(); decimal.setInt(val); decimalField.setText(decimal.getString()); hex.setInt(val); hexField.setText(hex.getString()); octal.setInt(val); octalField.setText(octal.getString()); } }