Java examples for Swing:JDialog
Sets up a FileDialog box that lets the user choose a file.
import java.applet.*; import java.awt.*; import java.io.*; import java.net.URL; import java.net.URI; public class Main{ /**/* www.ja va 2s . com*/ * 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 URL getSomeOldFile(Component C) { File myF = null; URL url = 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); } ; try { url = myF.toURI().toURL(); } catch (Exception e) { System.out.println("Error converting file to URL:" + myF + "\n" + e); } return url; } 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; } }