Infolinks In Text Ads

Jun 24, 2010

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;
}

1 comment:

  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

Related Posts Plugin for WordPress, Blogger...