forked from extra-programming/life
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentUtil.java
More file actions
executable file
·78 lines (66 loc) · 2.19 KB
/
ComponentUtil.java
File metadata and controls
executable file
·78 lines (66 loc) · 2.19 KB
1
// file ComponentUtil.java // package <default>// file ComponentUtil.java import java.applet.*;import java.awt.*;import java.io.*;public class ComponentUtil extends Object{ public static Frame getParent( Component theComponent ) { // this finds the topmost ancestor parent of a component,;; // so that we can give that parent to dialog constructors...;; Component currParent = theComponent; Frame theFrame = null; while ( currParent != null ) { if ( currParent instanceof Frame ) { theFrame = (Frame) currParent; break; }; currParent = currParent.getParent(); }; return theFrame; } // getParent /** * Sets up a dialog box that lets the user choose a file. * Have to copy in the ComponentUtil.java class to use the "getParent" * which is necessary for our FileDialog */ static File getSomeOldFile( Component C ) { File myF = null; Frame dFrame = ComponentUtil.getParent( C ); FileDialog myFDialog = new FileDialog( dFrame, "hiya" ); /* have to "import java.io.*;" to use FileDialog */; /* note: can append ",FileDialog.LOAD)" or ",FileDialog.SAVE)" to FileDialog constructor */; myFDialog.setVisible(true); // show(); deprecated try { myF = new File( myFDialog.getDirectory(), myFDialog.getFile() ); } catch( Exception exc ) { System.out.println( "Error opening file:" + exc ); }; return myF; } // getSomeOldFile /** * When given a file, this reads all its contents and puts them into a string */; static String readStringFromFile( File theFile ) { String myReturnString; if ( theFile == null ) { myReturnString = "No File was opened"; } else { try { FileInputStream in = new FileInputStream( theFile ); int size = (int) theFile.length(); byte[] data = new byte [ size ]; /* let's read in the file (which may not all arrive in one chomp...)*/; int bytes_read = 0; while ( bytes_read < size ) { bytes_read += in.read( data, bytes_read, size - bytes_read ); }; myReturnString = new String( data ); // used to be new String( data, 0 ); } catch( Exception exc ) { myReturnString = "Error opening file:" + exc; }; }; return myReturnString; } // readStringFromFile} // class ComponentUtil