浏览按钮选择目录 [英] Browse button to select directory
问题描述
我想在我的网页中创建一个浏览按钮来选择目录而不是文件.我知道输入类型文件在这里不起作用,但是有什么办法可以用 Javascript 来做到这一点.我想获取客户端机器的文件路径,这在 IE 中是可能的,但其他浏览器不支持,但这对我来说很好.
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.
下面是我用来从浏览器调用小程序的代码,但我从引导类路径中检测到:C:\PROGRA~1\Java\jre7\lib\deploy.jar 在浏览器中出错.我已经使用 Java 1.5 编译了类文件
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>
代码
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 ");
}
}
}
推荐答案
你到底为什么在 paint
方法中调用它?这很可能是在每次小程序painted
时尝试创建新窗口.
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();
/*...*/
相反,在您的 init
方法中创建一个 JButton
并附加一个 ActionListener
到它...
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);
}
您可能还想看看 Applet 可以做什么和不能做什么做
这篇关于浏览按钮选择目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!