import java.awt.event.*;
import java.awt.*;
import java.io.File;
import javax.swing.*;
public class FileChooserExample extends JFrame implements ActionListener
{
JButton b;
Container c;
JLabel lb;
FileChooserExample(){
c=getContentPane();
c.setLayout(new FlowLayout());
b=new JButton("open");
lb=new JLabel("");
b.addActionListener(this);
c.add(b); c.add(lb);
}
public void actionPerformed(ActionEvent e) {
JFileChooser j = new JFileChooser();
int n=j.showOpenDialog(this);
if(n == JFileChooser.APPROVE_OPTION){
File file = j.getSelectedFile();
System.out.println("File Selected: " + file.getName());
lb.setText("File Selected: " + file.getName());
}else{
System.out.println("Open command canceled");
}
}
public static void main(String[] args)
{
FileChooserExample ch=new FileChooserExample();
ch.setSize(400,400);
ch.setVisible(true);
ch.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
output:
0 Comments
Post a Comment