Spring Framework 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

thisFrame=this;

createAndShowGUI();

}



private void createAndShowGUI()

{



// Set frame properties

setTitle("File Dialog Example");

setSize(500,500);

setLayout(new FlowLayout());

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);



// 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

fd.setDirectory("E:\\java");



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

fd.setMode(jb.getSelectedIndex());



// Make it visible

fd.setVisible(true);

}

});



// Add JComboBox

add(jb);



// Add button

add(b);

}



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.