JFileChooser Another Example

Program

import javax.swing.JFrame;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.io.File;

class JFileChooserTest extends JFrame implements ActionListener{
  
    private JFileChooser fc;
    private JLabel jl;
            
    JFileChooserTest()
    {
        setTitle("JFileChooser Another Example");
        setLayout(new FlowLayout());
        setJFileChooser();
        setJLabel();
        setAction();
        setSize(700,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    void setJFileChooser()
    {
        fc = new JFileChooser();
        fc.setCurrentDirectory(new File("G://"));
        add(fc);
    }
    
    void setJLabel()
    {
        jl = new JLabel();
        add(jl);
    }
     
    void setAction()
    {
        fc.addActionListener(this);
    }
  
    public void actionPerformed(ActionEvent eve)
    {
        jl.setText(fc.getSelectedFile().getPath());
    }
}

public class Javaapp {
  
    public static void main(String[] args) {
      
        JFileChooserTest jf = new JFileChooserTest();
    }
}


Program Output

Leave a Comment