File Chooser Java applet code example

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;

public class Test extends JFrame
{

    public Test()
    {
        super("Simple File Chooser Application");
        chooser = new JFileChooser();
        button = new JButton("show file chooser ...");
        Container contentPane = getContentPane();
        contentPane.setLayout(new FlowLayout());
        contentPane.add(button);
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                chooser.setFileSelectionMode(1);
                int state = chooser.showOpenDialog(null);
                File file = chooser.getSelectedFile();
                if(file != null && state == 0)
                    JOptionPane.showMessageDialog(null, file.getPath());
                else
                if(state == 1)
                    JOptionPane.showMessageDialog(null, "Canceled");
            }

        });
    }

    public static void main(String args[])
    {
        JFrame f = new Test();
        f.setBounds(300, 300, 350, 100);
        f.setVisible(true);
        f.setDefaultCloseOperation(2);
        f.addWindowListener(new WindowAdapter() {

            public void windowClosed(WindowEvent e)
            {
                System.exit(0);
            }

        });
    }

    JFileChooser chooser;
    JButton button;
}

Comments

  1. This is a nice example. But the filechooser is not going inside of the directory. If we are copying the path in the folder name text area, it is opening. No other way to open by double clicking on folder icons.Also i n top row up one level option is there. But down level is not there.

    ReplyDelete

Post a Comment

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

Use of req.query, req.params and req.body in NODE JS

How to capture finger prints in java