/** *A short description : Class for simplified GUI Dialog input of all variable types .....Getting GUI Dialog input *in java is a very tedious process ! This java *class will help you to get all types of variables input from GUI Dialog with relatively ease. *All nine fundamental variable types are covered : byte, short, int, long, float, double, char, String, and *boolean . It ignores appropiate input errors smartly. Copy this file to your project/java directory and *name it gui.java . Compile it using command : javac gui.java , and you are all done. In BlueJ *Save the source file, then use the "Import Class" function in BlueJ to import this class into your project. *Then it can be used like any other class. Please see the use of this class and input methods in *testguinput.java class example below, if you enjoy my effort, let me know through e-mail !!! * * Original Authors : Bruce Quig + Michael Kolling, version: 1.0, Date: 04.03.1999 * @author (Modified and enhanced by Shirshasin Ghosh Of Naihati, West Bengal, India... shirsha@rediffmail.com ) * @version ( 2.0 : 12.8.2004 : 11:30 A.M ) */ import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JDialog; import javax.swing.JButton; import java.awt.event.*; public class gui { /* instance variables */ final String STRING_TITLE = "Enter a String"; final String CHAR_TITLE = "Enter a charecter"; final String INT_TITLE = "Enter an integer number"; final String BOOLEAN_TITLE = "Select True or False / Yes or No"; final String FLOAT_TITLE = "Enter a floating point number"; final String TRUE = "True"; final String FALSE = "False"; final String EMPTY_STRING = ""; public boolean checkecc = false ; public int i , j ; public String hold = "" ; public char a ; /* 1. input a byte number */ public byte getByte(String prompt) { Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING}; Object[] options = { "OK" }; String inputValue = ""; boolean validResponse = false; byte response = 0; while(!validResponse) { final JOptionPane optionPane = new JOptionPane(commentArray, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); optionPane.setWantsInput(true); JDialog dialog = optionPane.createDialog(null, "Enter a Byte number"); dialog.pack(); dialog.show(); String result = (String)optionPane.getInputValue(); if ( checkecc ) result = convertwhole(result) ; try { response = Byte.parseByte(result); validResponse = true; } catch(NumberFormatException exception) { if(result.equals("uninitializedValue")) result = ""; commentArray[1] = "Invalid byte: " + result; commentArray[2] = "Enter a valid byte"; } } return response; } /* End of getByte method */ /*2. input a short number */ public short getShort(String prompt) { Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING}; Object[] options = { "OK" }; String inputValue = ""; boolean validResponse = false; short response = 0; while(!validResponse) { final JOptionPane optionPane = new JOptionPane(commentArray, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); optionPane.setWantsInput(true); JDialog dialog = optionPane.createDialog(null, "Enter a short number"); dialog.pack(); dialog.show(); String result = (String)optionPane.getInputValue(); if ( checkecc ) result = convertwhole(result) ; try { response = Short.parseShort(result); validResponse = true; } catch(NumberFormatException exception) { if(result.equals("uninitializedValue")) result = ""; commentArray[1] = "Invalid short: " + result; commentArray[2] = "Enter a valid short"; } } return response; } /* End of getShort method */ /* 3. Input a integer */ public int getInt(String prompt) { Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING}; Object[] options = { "OK" }; String inputValue = ""; boolean validResponse = false; int response = 0; while(!validResponse) { final JOptionPane optionPane = new JOptionPane(commentArray, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); optionPane.setWantsInput(true); JDialog dialog = optionPane.createDialog(null, INT_TITLE); dialog.pack(); dialog.show(); String result = (String)optionPane.getInputValue(); if ( checkecc ) result = convertwhole(result) ; try { response = Integer.parseInt(result); validResponse = true; } catch(NumberFormatException exception) { if(result.equals("uninitializedValue")) result = ""; commentArray[1] = "Invalid int: " + result; commentArray[2] = "Enter a valid integer"; } } return response; } /* End of getInt method */ /* 4. input a long number */ public long getLong(String prompt) { Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING}; Object[] options = { "OK" }; String inputValue = ""; boolean validResponse = false; long response = 0; while(!validResponse) { final JOptionPane optionPane = new JOptionPane(commentArray, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); optionPane.setWantsInput(true); JDialog dialog = optionPane.createDialog(null, "Enter a long number"); dialog.pack(); dialog.show(); String result = (String)optionPane.getInputValue(); if ( checkecc ) result = convertwhole(result) ; try { response = Long.parseLong(result); validResponse = true; } catch(NumberFormatException exception) { if(result.equals("uninitializedValue")) result = ""; commentArray[1] = "Invalid long : " + result; commentArray[2] = "Enter a valid long"; } } return response; } /* End of getLong method */ /* 5. Input a Float */ public float getFloat(String prompt) { Object[] options = { "OK" }; Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING}; String inputValue = ""; boolean validResponse = false; float response = 0.0f; while(!validResponse) { final JOptionPane optionPane = new JOptionPane(commentArray, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); optionPane.setWantsInput(true); JDialog dialog = optionPane.createDialog(null, FLOAT_TITLE); dialog.pack(); dialog.show(); String result = (String)optionPane.getInputValue(); if ( checkecc ) result = convertfrac(result) ; try { response = Float.valueOf(result).floatValue(); validResponse = true; } catch(NumberFormatException exception) { commentArray[1] = "Invalid float: " + result; commentArray[2] = "Enter a valid float"; inputValue = result; } } return response; } /* End of getFloat method */ /* 6. input a double */ public double getDouble(String prompt) { Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING}; Object[] options = { "OK" }; String inputValue = ""; boolean validResponse = false; double response = 0; while(!validResponse) { final JOptionPane optionPane = new JOptionPane(commentArray, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); optionPane.setWantsInput(true); JDialog dialog = optionPane.createDialog(null, "Enter a double precision number"); dialog.pack(); dialog.show(); String result = (String)optionPane.getInputValue(); if ( checkecc ) result = convertfrac(result) ; try { response = Double.parseDouble(result); validResponse = true; } catch(NumberFormatException exception) { if(result.equals("uninitializedValue")) result = ""; commentArray[1] = "Invalid double : " + result; commentArray[2] = "Enter a valid double"; } } return response; } /* End of getDouble method */ /* 7. Input a char */ public char getChar(String prompt) { char response ='-'; String result = null; Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING}; Object[] options = { "OK" }; String inputValue = ""; boolean validResponse = false; while(!validResponse) { final JOptionPane optionPane = new JOptionPane(commentArray, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); optionPane.setWantsInput(true); JDialog dialog = optionPane.createDialog(null, CHAR_TITLE); dialog.pack(); dialog.show(); Object input = optionPane.getInputValue(); if(input != JOptionPane.UNINITIALIZED_VALUE) { result = (String)input; if(result != null && result.length() == 1) { response = result.charAt(0); validResponse = true; } else { commentArray[1] = "Invalid entry : " + result; commentArray[2] = "Enter a single character"; } } else { commentArray[1] = "Invalid entry : " + result; commentArray[2] = "Enter a single character"; } } return response; } /* End of getChar method */ /* 8. Input a String */ public String getString(String prompt) { Object[] commentArray = {prompt, EMPTY_STRING, EMPTY_STRING}; Object[] options = { "OK" }; String inputValue = ""; boolean validResponse = false; String result = null; while(!validResponse) { final JOptionPane optionPane = new JOptionPane(commentArray, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); optionPane.setWantsInput(true); JDialog dialog = optionPane.createDialog(null, STRING_TITLE); dialog.pack(); dialog.show(); Object response = optionPane.getInputValue(); if(response != JOptionPane.UNINITIALIZED_VALUE) { result = (String)response; validResponse = true; } else { commentArray[1] = "Invalid entry : " + result; commentArray[2] = "Enter a valid String"; } } return result; } /* End of getString method */ /* 9. Input a Boolean */ public boolean getBoolean(String prompt, String trueText, String falseText) { Object[] commentArray = {prompt, EMPTY_STRING}; boolean validResponse = false; int result = -1; while(!validResponse) { Object[] options = {trueText, falseText}; result = JOptionPane.showOptionDialog(null, commentArray, BOOLEAN_TITLE, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, //don't use a custom Icon options, //the titles of buttons TRUE ); //the title of the default button // check true or false buttons pressed if(result == 0 || result == 1) validResponse = true; else commentArray[1] = "Incorrect selection : Choose true or false buttons"; } return (result == 0); } /* End of getBoolean mehod */ /* overloading getBoolean method */ public boolean getBoolean(String prompt) { return getBoolean(prompt, TRUE, FALSE); } /* end of overloaded getBoolean method */ /*** 10. Two ecc validate and triming/removing functions are giving below ***/ public String convertfrac (String str1) { hold = "0" ; a = ' '; str1 = str1.trim() ; i = str1.length() ; int count = 0 ; for ( j = 0 ; j < i ; j++ ) { a = str1.charAt(j) ; if ( a == '.' ) count++ ; if ( ( Character.isDigit(a) || ( a == '.') ) && ( count <= 1) ) hold = hold + a ; } return hold ; } public String convertwhole (String str1) { str1 = str1.trim() ; i = str1.length() ; hold = "0" ; a = ' '; for ( j = 0 ; j < i ; j++ ) { a = str1.charAt(j) ; if ( Character.isDigit(a) ) hold = hold + a ; else if ( a == '.' ) return hold ; } return hold ; } /* 12. A Handy method for testing prime numbers */ public boolean isprime( long b ) { long a, c; byte k = 1 ; a = Math.round( Math.sqrt(b) ) ; for ( c = 2; c <= a ; c++ ) { if ((b % c) == 0 ) { k = 0 ; break ; } } if (k == 1) return true ; else return false ; } /* End of isprime method */ /* 13A. A handy method for giving spaces on a line */ public void space ( int n ) { int i ; for ( i = 1 ;i <= n ; i++) System.out.print(" "); } /* End of space method */ /* 13B. Simple blank line showing method */ public void showline( int n ) { for ( i = 1 ; i <= n ; i++) System.out.print("\n") ; } /* 14. Set the ecc code to true of false */ public void ecc( boolean get ) { checkecc = get ; } /* 15. Simple message display method */ public void show( String prompt ) { JOptionPane.showMessageDialog(null, prompt) ; } /* 16. show method overloaded for heading */ public void show( String prompt, String heading ) { JOptionPane.showMessageDialog(null, prompt, heading, JOptionPane.INFORMATION_MESSAGE) ; } } /* End of gui class */