// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // File Name : InputPanel.java // Purpose : Provides a standard input panel for the temp. converter // Author : B.J. Johnson // Date : 20-Jul-2005 // Description: This class provides a standard input panel for the TempConv // temperature conversion program. The panel extends the // swing JPanel component, and comprises to parts. One is // a label that indicates the temperature scale for this // panel. The other is a JTextField into which the value of // the temperature can be typed. // Notes : // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // COPYRIGHT (C) LOYOLA MARYMOUNT UNIVERSITY // // Written by B.J. Johnson (bjohnson05@gmail.com), All rights reserved // Offered as freeware, under GPL // // Refer to: http://www.gnu.org/licenses/gpl.html to obtain a copy of the // Gnu Public License, and read the details // // This software is offered without warranty of any kind; the author // and/or copyright holder is not responsible for any damage to hardware // or any unexpected results that may occur from its use. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Revision History // ---------------- // // Rev Rev. Date Modified by: Description of change // ----- ----------- ------------ -------------------------------------- // 1.0-0 20-Jul-2005 B.J. Johnson Initial coding and capture // // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // IMPORTS AND PACKAGES // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // CLASS DEFINITION // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public class InputPanel extends JPanel { private JLabel whatAmI; private JTextField myData; private TempConvApp parent; private char converter; // Constructor takes a string for the label to display public InputPanel( String label ) { whatAmI = new JLabel( label ); myData = new JTextField( 10 ); whatAmI.setText( label ); this.setLayout( new GridLayout( 1, 2 ) ); this.add( whatAmI ); this.add( myData ); } // Constructor takes a label and an instance of the parent TempConv class public InputPanel( String label, TempConvApp mom, char which ) { whatAmI = new JLabel( label ); myData = new JTextField( 10 ); parent = mom; converter = which; whatAmI.setText( label ); // Add an action listener to fire the conversion/display method myData.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { String lastAction = ae.getActionCommand(); parent.convertme( converter ); } } ); this.setLayout( new GridLayout( 1, 2 ) ); this.add( whatAmI ); this.add( myData ); } // This method returns the value of the text field as a double. If the // "Enter" key was pressed in an empty field, the value of 0.0 will be // assumed to prevent NumberFormat exceptions. public double getValue() { double realValue; String value = myData.getText(); try { realValue = Double.parseDouble( value ); } catch( NumberFormatException nfe ) { realValue = 0; } return( realValue ); } // This method set the value of the text field from a double public void setValue( double newValue ) { myData.setText( Double.toString( newValue ) ); } // This method returns the value of the text field as a double public void clearField() { myData.setText( "" ); } // The main method is just for testing. public static void main( String[] args ) { JFrame jf = new JFrame(); InputPanel fp = new InputPanel( new String( "Blah blah blah: " )); jf.getContentPane().add( fp ); jf.setSize( 275, 100 ); jf.setLocation( 500, 500 ); jf.setVisible( true ); // Add a window listener to close the window jf.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } }); } }