浏览按钮选择目录 [英] Browse button to select directory

查看:115
本文介绍了浏览按钮选择目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创造我的网页的浏览按​​钮选择目录,而不是文件。我知道的输入类型的文件不会在这里工作,但有什么办法使用Javascript做到这一点。我想获得客户机的文件路径是可能的,但IE浏览器等不配套,但是这是为我好。

我被困的方式是如何让按钮文件目录。

下面是code我使用调用浏览器从applet的但我正在逐渐从引导类路径检测:C:\\ PROGRA〜1 \\的Java \\ jre7 \\ lib目录\\ deploy.jar错误的浏览器。我编了使用Java 1.5类文件

<小程序code =com.life.draw.BrowsePage.class>< /小程序 - GT ;

code

 公共类BrowsePage扩展JApplet的{
@覆盖
公共无效漆(图形G){
    // TODO自动生成方法存根
    JFileChooser的选择器=新的JFileChooser();
    chooser.setCurrentDirectory(新的java.io.File()。);
    chooser.setDialogTitle(浏览来处理该文件夹);
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setAcceptAllFileFilterUsed(假);    如果(chooser.showOpenDialog(空)== JFileChooser.APPROVE_OPTION){
        的System.out.println(getCurrentDirectory():+ chooser.getCurrentDirectory());
        的System.out.println(getSelectedFile():+ chooser.getSelectedFile());
    }其他{
        的System.out.println(不选择);
    }
}
}


解决方案

凭啥你在你的油漆调用这个的方法?这可能是试图创建以创建新窗口每次applet是彩绘

 公共无效漆(图形G){
    // TODO自动生成方法存根
    JFileChooser的选择器=新的JFileChooser();
    /*...*/

相反,你的的init 方法创建一个的JButton 并附加的ActionListener 吧...

 公共无效的init(){
    的setLayout(新的GridBagLayout());
    JButton的浏览=的新的JButton(...);
    browse.addActionListener(新的ActionListener(){
        公共无效的actionPerformed(ActionEvent的EVT){
            JFileChooser的选择器=新的JFileChooser();
            chooser.setCurrentDirectory(新的java.io.File()。);
            chooser.setDialogTitle(浏览来处理该文件夹);
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            chooser.setAcceptAllFileFilterUsed(假);            如果(chooser.showOpenDialog(空)== JFileChooser.APPROVE_OPTION){
                的System.out.println(getCurrentDirectory():+ chooser.getCurrentDirectory());
                的System.out.println(getSelectedFile():+ chooser.getSelectedFile());
            }其他{
                的System.out.println(不选择);
            }
        }
    });
    加(浏览);
}

您也可能想看看什么小程序,不能做

I want to create a browse button in my web page to select directory and not file. I know that input type file won't work here but is there any way to do it with Javascript. I want to get the filepath of client machine which is possible in IE but other browser are not supporting but that is fine for me.

The way I got stuck is how to get file directory in button.

Below is the code I am using to call applet from browser but I am getting Detected from bootclasspath: C:\PROGRA~1\Java\jre7\lib\deploy.jar error in browser. I have compiled class file using Java 1.5

<applet code="com.life.draw.BrowsePage.class"></applet>

Code

public class BrowsePage extends JApplet {
@Override
public void paint(Graphics g) {
    // TODO Auto-generated method stub
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new java.io.File("."));
    chooser.setDialogTitle("Browse the folder to process");
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setAcceptAllFileFilterUsed(false);

    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        System.out.println("getCurrentDirectory(): "+ chooser.getCurrentDirectory());
        System.out.println("getSelectedFile() : "+ chooser.getSelectedFile());
    } else {
        System.out.println("No Selection ");
    }
}
}

解决方案

Why the hell are you calling this in the your paint method? This is likely trying creating to create new windows EVERY TIME the applet is painted.

public void paint(Graphics g) {
    // TODO Auto-generated method stub
    JFileChooser chooser = new JFileChooser();
    /*...*/

Instead, create a JButton in your init method and attach an ActionListener to it...

public void init() {
    setLayout(new GridBagLayout());
    JButton browse = new JButton("...");
    browse.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(new java.io.File("."));
            chooser.setDialogTitle("Browse the folder to process");
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            chooser.setAcceptAllFileFilterUsed(false);

            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                System.out.println("getCurrentDirectory(): "+ chooser.getCurrentDirectory());
                System.out.println("getSelectedFile() : "+ chooser.getSelectedFile());
            } else {
                System.out.println("No Selection ");
            }
        }
    });
    add(browse);
}

You might also like to take a look at What Applets Can and Cannot Do

这篇关于浏览按钮选择目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆