Explore Java Hacks and Tutorials
Be Social. Be Updated.

Example on AWT FileDialog

This illustrates a small example on the java.awt.FileDialog, the class that does the job of a JFileChooser in Swing i.e. opening and/or saving files. In some cases, we can find that FileDialog is much better than the swing JFileChooser because, JFileChooser lacks the 2 main things, auto complete and the file context menu by default. But still programmers prefer using JFileChooser and they try to crack it to get the features they want. Whatsoever, though FileDialog has got these awesome features, it is platform dependent unlike JFileChooser and is developed using the native C/C++ code. In this example i'll be discussing with an example on FileDialog.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class FileDialogExample extends JFrame
JFrame thisFrame;
JComboBox jb;
JButton b;
public FileDialogExample()
// Store current object in thisFrame

private void createAndShowGUI()

// Set frame properties
setTitle("File Dialog Example");
setLayout(new FlowLayout());

// Create JComboBox
jb=new JComboBox();

// Add items to the frame
jb.addItem("LOAD"); // 0 index
jb.addItem("SAVE"); // 1 index

b=new JButton("Fire!");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
// Create a FileDialog whose parent is this frame and with title selected in JComboBox
FileDialog fd=new FileDialog(thisFrame,(String)jb.getSelectedItem());

// Set a dir

// Set FileDialog.LOAD=0 or FileDialog.SAVE=1

// Make it visible

// Add JComboBox

// Add button

public static void main(String args[])
new FileDialogExample();

Output of the program

Screenshot of FileDialog in AWT

I think everything is understood right from the program itself with the help of comments. This is just a simple example of FileDialog in AWT.

Posted by at on

Tags: Abstract Window Toolkit