java实现选择文件夹,在Java中选择文件夹目标?

I am a newbie to Java. I am trying to dynamically choose the file location to save the outcome of my project (to be initiated at the very start of my project). I worked around with a few FileDialog examples, but each one of them allows me to choose a file and not a folder.

Can anyone please help me with an example (or) link to one for the same?

解决方案

You could try something like this (as shown here: Select a Directory with a JFileChooser):

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import java.util.*;

public class DemoJFileChooser extends JPanel

implements ActionListener {

JButton go;

JFileChooser chooser;

String choosertitle;

public DemoJFileChooser() {

go = new JButton("Do it");

go.addActionListener(this);

add(go);

}

public void actionPerformed(ActionEvent e) {

chooser = new JFileChooser();

chooser.setCurrentDirectory(new java.io.File("."));

chooser.setDialogTitle(choosertitle);

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

//

// disable the "All files" option.

//

chooser.setAcceptAllFileFilterUsed(false);

//

if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {

System.out.println("getCurrentDirectory(): "

+ chooser.getCurrentDirectory());

System.out.println("getSelectedFile() : "

+ chooser.getSelectedFile());

}

else {

System.out.println("No Selection ");

}

}

public Dimension getPreferredSize(){

return new Dimension(200, 200);

}

public static void main(String s[]) {

JFrame frame = new JFrame("");

DemoJFileChooser panel = new DemoJFileChooser();

frame.addWindowListener(

new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

}

);

frame.getContentPane().add(panel,"Center");

frame.setSize(panel.getPreferredSize());

frame.setVisible(true);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值