精华内容
下载资源
问答
  • JFileChooser

    2010-10-31 12:36:11
    JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.showSaveDialog(null);
  • I would like to be able to control the appearance of the JFileChooser. In particular I would like to save how the JFileChooser was displayed when it was last shown. I would like to save whether it was...

    I would like to be able to control the appearance of the JFileChooser. In particular I would like to save how the JFileChooser was displayed when it was last shown. I would like to save whether it was used in details/list view and which column (eg, size or date modified) the lists were sorted.

    I know there is a lot of questions about JFileChooser but I have not been able to find what I am looking for.

    Thanks

    EDIT: it was suggested as an answer, but keeping a reference of the filechooser is not suffient, as I want to persist the settings across many times that I run the application

    EDIT: for example I usually want to open the most recent file I have downloaded, so I want to sort by date modified and display in detail view

    解决方案

    Keep a reference to it and only construct it once. It should open on subsequent occasions looking pretty much like it did when the user disposed it. You would need to take extra steps to restore the location of the file chooser.

    Persisting between runs.

    There are a number of ways of storing the data between runs (e.g. a properties file, XML, Preferences etc.). This is the quick'n'dirty way to achieve it.

    import java.awt.event.*;

    import javax.swing.*;

    import java.io.*;

    class SerializeMyChooser {

    static private JFileChooser fileChooser;

    static File serializedChooser = new File("chooser.ser");

    public static void main(String[] args) {

    SwingUtilities.invokeLater( new Runnable() {

    public void run() {

    final JButton showChooser = new JButton("Open File");

    showChooser.addActionListener( new ActionListener() {

    public void actionPerformed(ActionEvent ae) {

    if (fileChooser==null) {

    if (serializedChooser.exists()) {

    // use the resialized form

    try {

    ObjectInputStream ois = new ObjectInputStream(

    new FileInputStream(serializedChooser));

    fileChooser = (JFileChooser)ois.readObject();

    ois.close();

    } catch(Exception e) {

    // something SNAFU - use fall-back

    fileChooser = new JFileChooser();

    // configure file chooser..

    }

    } else {

    fileChooser = new JFileChooser();

    // configure file chooser..

    }

    }

    fileChooser.showOpenDialog(showChooser);

    }

    });

    JOptionPane.showMessageDialog(null, showChooser);

    if (fileChooser!=null) {

    try {

    ObjectOutputStream oos = new ObjectOutputStream(

    new FileOutputStream(serializedChooser));

    oos.writeObject(fileChooser);

    oos.flush();

    oos.close();

    } catch(Exception e) {

    e.printStackTrace();

    }

    }

    }

    });

    }

    }

    Correct I/O & exception handling is left as an exercise for the user.

    展开全文
  • JFileChooser 运用

    2012-12-10 16:03:03
    选择路径保存文件,运用JFileChooser组件生成、
  • JFileChooser api

    2019-05-29 15:06:04
    JFileChooser() 构造一个指向用户默认目录的JFileChooserJFileChooser(FilecurrentDirectory) 给使用定的File作为路径来构造一个JFileChooser。 setFileSelectionMode(intmode) 设置JFileChooser,.....
    JFileChooser() 
              构造一个指向用户默认目录的JFileChooser
    JFileChooser(File currentDirectory)
              给使用定的File作为路径来构造一个JFileChooser

     

    setFileSelectionMode(int mode)
              设置JFileChooser,以允许用户只选择文件,只选择目录,或者可选择文件和目录。

    模式参数:FILES_AND_DIRECTORIES   指示显示文件和目录。

          FILES_ONLY                             指示仅显示文件。

         DIRECTORIES_ONLY                指示仅显示目录。

    showDialog(Component parent,String approveButtonText)
              弹出具有自定义approve按钮的自定义文件选择器对话框。

    showOpenDialog(Component parent)
              弹出一个“Open File”文件选择器对话框。

    showSaveDialog(Component parent)
              弹出一个“Save File”文件选择器对话框。

    setMultiSelectionEnabled(boolean b)
              设置文件选择器,以允许选择多个文件。

    getSelectedFiles() 
              如果将文件选择器设置为允许选择多个文件,则返回选中文件的列表(文件[])。

    getSelectedFile()
              返回选中的文件。

     

     

    
     
    1. package com.liang;

    2.  
    3. import java.awt.event.ActionEvent;

    4. import java.awt.event.ActionListener;

    5. import java.io.File;

    6.  
    7. import javax.swing.JButton;

    8. import javax.swing.JFileChooser;

    9. import javax.swing.JFrame;

    10. import javax.swing.JLabel;

    11.  
    12. public class FileChooser extends JFrame implements ActionListener{

    13. JButton open=null;

    14. public static void main(String[] args) {

    15. new FileChooser();

    16. }

    17. public FileChooser(){

    18. open=new JButton("open");

    19. this.add(open);

    20. this.setBounds(400, 200, 100, 100);

    21. this.setVisible(true);

    22. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    23. open.addActionListener(this);

    24. }

    25. @Override

    26. public void actionPerformed(ActionEvent e) {

    27. // TODO Auto-generated method stub

    28. JFileChooser jfc=new JFileChooser();

    29. jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );

    30. jfc.showDialog(new JLabel(), "选择");

    31. File file=jfc.getSelectedFile();

    32. if(file.isDirectory()){

    33. System.out.println("文件夹:"+file.getAbsolutePath());

    34. }else if(file.isFile()){

    35. System.out.println("文件:"+file.getAbsolutePath());

    36. }

    37. System.out.println(jfc.getSelectedFile().getName());

    38.  
    39. }

    40.  
    41. }


     

     

    JFileChooser效果图如下:

    展开全文
  • java JFileChooser

    2018-11-14 02:09:00
    FileDialog 在Windows下无法用FilenameFilter 且需要Frame... JFileChooser fileChooser=new JFileChooser();//创建对象 fileChooser.setCurrentDirectory(new File("."));//当前显示目录 为工程目录 fileChooser.se...

    FileDialog 在Windows下无法用FilenameFilter 且需要Frame

    所有选择JFileChooser

    JFileChooser fileChooser=new JFileChooser();//创建对象
        fileChooser.setCurrentDirectory(new File("."));//当前显示目录 为工程目录
        fileChooser.setAcceptAllFileFilterUsed(false); //是否显示所有文件
        fileChooser.addChoosableFileFilter(new FileFilter() {
       @Override
       public boolean accept(File f) {
      //返回true 表示显示 false不显示 getName返回该文件当前目录下所有的文件夹和文件
      //判断该文件是文件夹
      if(f.getName().endsWith(".java")||f.isDirectory()) {
      return true;
      }
       return false;
      }
      @Override
      public String getDescription() {
      //设置文件栏的类型里文字
      return "JAVA源程序(*.java)";
     }
    });
    //监听事件获取选择文件
      fileChooser.addActionListener(new ActionListener() {
     @Override
      public void actionPerformed(ActionEvent e) {
      File selectFile=fileChooser.getSelectedFile(); //选择文件
     System.out.println(selectFile.getAbsolutePath());
    }
    });
    fileChooser.showOpenDialog(null); //打开文件选择框
    fileChooser.showSaveDialog(null);//保存文件选择框
    }

    转载于:https://www.cnblogs.com/peichao/p/9955869.html

    展开全文
  • I want to have a button with a JFileChooser action. This is the code that I wrote:public class Main {private static String fullPath;private JFileChooser inputFile;public static void main(String args[]...

    I want to have a button with a JFileChooser action. This is the code that I wrote:

    public class Main {

    private static String fullPath;

    private JFileChooser inputFile;

    public static void main(String args[]) throws FileNotFoundException, IOException {

    try {

    GridBagConstraints gbc = new GridBagConstraints();

    JButton inputButton = new JButton("Browse input file");

    myPanel.add(inputButton, gbc);

    inputButton.addActionListener(new ActionListener() {

    public void ActionPerformed(ActionEvent e) {

    JFileChooser inputFile = new JFileChooser();

    inputFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

    File file1 = inputFile.getSelectedFile();

    String fullpathTemp = (String) file1.getAbsolutePath();

    fullPath = fullpathTemp;

    }

    public void actionPerformed(ActionEvent e) {

    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

    }

    });

    } catch (Exception e) {

    System.err.println("Error: " + e.getMessage());

    } finally {

    }

    }

    }

    but the problem is that when I run it, I got a long error message that is part of:

    Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.

    at main.Main$1.actionPerformed(Main.java:200)

    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)

    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)

    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)

    解决方案

    The ActionListener here is explicitly throwing an UnsupportedOperationException. Move the JFileChooser functionality into the ActionListener:

    input_button.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {

    JFileChooser inputFile = new JFileChooser();

    inputfile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

    if (inputfile.showOpenDialog(myFrame) == JFileChooser.APPROVE_OPTION) {

    File file1 = inputFile.getSelectedFile();

    String fullpathTemp = (String) file1.getAbsolutePath();

    ...

    }

    }

    });

    展开全文
  • buffer JFileChooser

    2018-04-19 11:25:03
    import javax.swing.JFileChooser; public class Main { public static void main(String[] args) throws IOException { StringBuffer buffer = new StringBuffer(); BufferedReader br; ...
  • 主要介绍了Java Swing组件JFileChooser用法,结合实例形式分析了java Swing组件JFileChooser文件选择器的功能、使用方法及相关注意事项,需要的朋友可以参考下
  • I have following code. It saves file but with empty content. What's wrong with it?public void saveMap() {String sb = "TEST ...JFileChooser chooser = new JFileChooser();chooser.setCurrentDirector...
  • JFileChooser选择框

    2020-10-16 21:39:31
    文章目录前言一、JFileChooser总结 前言 一、JFileChooser 弹出选择框,并返回选择的文件绝对路径(非文件夹) //调出文件选择框 并返回选择的文件路径 public static String getFilePath() { //默认打开C盘,...
  • 主要介绍了Java文件选择对话框JFileChooser使用详解的相关资料,需要的朋友可以参考下
  • How to display default system icon for files in JFileChooser? i.e. the icons of the files in JFileChooser should be the same as the icons that appear on desktop and explorer?For example, NetBeans ico....

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 5,750
精华内容 2,300
关键字:

jfilechooser