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
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.
No comments:
Post a Comment