⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filechooserdemo.java

📁 文件浏览框
💻 JAVA
字号:
package palmsecuresdk.sample.data;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class FileChooserDemo extends JFrame {
    private JButton btn1, btn2, btn3, btn4;

    private JTextArea area;

    private ObjectOutputStream output;

    private JScrollPane scroll;

    private JPanel panel;

    public FileChooserDemo(){
        super("FileChooser Demo");

        btn1 = new JButton("Open Directory");
        btn1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser choose = new JFileChooser();
                choose.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

                int result = choose.showOpenDialog(null);

                if (result == JFileChooser.APPROVE_OPTION)// if approve (yes,
                // ok) is chosen
                {
                    File file = choose.getSelectedFile();

                    if (!file.exists())
                        area.append("File does not exist~!");

                    else if (file.isDirectory()) {
                        area.append("Directory " + file.getName()
                                + " containt following files:  ");
                        for (int i = 0; i < file.list().length; i++)
                            area.append(file.list()[i] + " ");
                    } else if (file.isFile())
                        area
                                .append("It is a file, please use the second button to hava another test ");
                } else
                    return;
            }
        });

        btn2 = new JButton("Open a file");
        btn2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser choose = new JFileChooser();
                choose.setFileSelectionMode(JFileChooser.FILES_ONLY);

                int result = choose.showOpenDialog(null);

                if (result == JFileChooser.APPROVE_OPTION)// if approve (yes,
                // ok) is chosen
                {
                    File file = choose.getSelectedFile();

                    if (!file.exists())
                        area.append("File does not exist~!");

                    else {
                        try {
                            output = new ObjectOutputStream(
                                    new FileOutputStream(file));
                            // ..
                            // tobeContinued
                        } catch (IOException ioexception) {

                            area.append(ioexception.toString());
                        }
                    }
                } else
                    return;
            }
        });

        btn3 = new JButton("Images File Filter");
        btn3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser choose = new JFileChooser();

                // add ImagePreviewPanel Accessory
                ImagePreviewPanel preview = new ImagePreviewPanel();
                choose.setAccessory(preview);
                choose.addPropertyChangeListener(preview);
                choose.setFileSelectionMode(JFileChooser.FILES_ONLY);

                /**//*
                 * //add FindAccessory FindAccessory search = new
                 * FindAccessory(); choose.setAccessory(search);
                 * choose.addPropertyChangeListener(search);
                 * choose.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                 */

                // add Filters
                String htmlExts[] = { "html", "htm" };
                String imageExts[] = { "jpg", "jpeg", "png", "gif","bmp" };

                // construct it giving it an array of file
                // extensions and a description string
                GenericFileFilter filter = new GenericFileFilter(htmlExts,
                        "HTML Files");
                choose.addChoosableFileFilter(filter);

                filter = new GenericFileFilter(imageExts,
                        "Images Files(*.jpg;*.jpeg;*.png;*.gif;*.bmp)");
                choose.addChoosableFileFilter(filter);

                choose.setFileView(new GenericFileView(imageExts,
                        GenericFileView.IMAGE_FILEVIEW));

                /**//*
                 * addChoosableFileFilter(Filter filter) and
                 * setFileFilter(Filter filter) are two familiar methods,they
                 * both add the filter to the JFileChooser, the difference is
                 * the setFileFilter set itself to the default choose.
                 */

                int result = choose.showOpenDialog(null);

                if (result == JFileChooser.APPROVE_OPTION)// if approve (yes,
                // ok) is chosen
                {
                    File file = choose.getSelectedFile();

                    if (!file.exists())
                        area.append("File does not exist~!");

                    else
                        area.append("You chose " + file.getName() + " ");
                } else
                    return;
            }
        });

        area = new JTextArea();
        scroll = new JScrollPane(area);
        Container cot = getContentPane();
        cot.add(scroll, BorderLayout.CENTER);

        panel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(btn1);
        panel.add(btn2);
        panel.add(btn3);

        cot.add(panel, BorderLayout.NORTH);

        setSize(400, 300);
        setVisible(true);
    }

    public static void main(String args[]) {
        FileChooserDemo app = new FileChooserDemo();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -