import java.awt.*; import java.awt.event.*; public class selectfile extends Frame implements ActionListener { FileDialog fileDialog; Button openFileButton; TextField result; public Frame frame; public selectfile () { Frame frame = new Frame(); frame.setLayout(new BorderLayout()); frame.setSize(700,200); frame.setLocation(100,100); openFileButton = new Button("Open File"); openFileButton.addActionListener(this); frame.add("Center",openFileButton); result = new TextField("selected file", 80); result.setEditable ( false ); result.setFont(new Font ("Dialog", Font.BOLD, 18)); frame.add ( "South", result ); frame.addWindowListener ( new CloseWindow()); frame.show(); } public void actionPerformed(ActionEvent event) { String buttonLabel = event.getActionCommand(); if (buttonLabel.equals("Open File")) { fileDialog = new FileDialog(this, "Open File", FileDialog.LOAD); // or they can specify FileDialog.LOAD and FileDialog.SAVE like this // fileDialog.setMode(FileDialog.LOAD); // fileDialog.setMode(FileDialog.SAVE); fileDialog.show(); String filename = fileDialog.getDirectory()+fileDialog.getFile(); result.setText(filename); /* in other applications, you could use this to open a file: * File fi = new File(fd.getDirectory(), fd.getFile()) */ } else { } } public static void main(String[] args) { selectfile fd; fd = new selectfile(); } } // responds to an event generated by the close window button class CloseWindow extends WindowAdapter { public void windowClosing (WindowEvent e) { e.getWindow().setVisible (false); System.exit(0); } }