在Java下如何做文件夹选择对话框,注意:不是文件选择对话框

lifeequation 2006-12-21 07:02:11
Java中的JFileChooser没有提供文件夹选择的功能,而我想要的是文件夹目录选择的对话框,请问哪位DX能帮帮忙?跪谢。
...全文
926 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
diggywang 2006-12-22
  • 打赏
  • 举报
回复
楼上的也是一种方法,l2fprod中有个控件就叫JDirectoryChooser,可以实现楼主需要的效果;不过我觉得用jfilechooser,设定DIRECTORIES_ONLY模式,效果是一样的
gtlang78 2006-12-21
  • 打赏
  • 举报
回复
http://common.l2fprod.com/
fool_leave 2006-12-21
  • 打赏
  • 举报
回复
Swing库的文件夹选择只有一个JFileChooser
除非你自己用Swing组件拼装
lifeequation 2006-12-21
  • 打赏
  • 举报
回复
谢谢fool_leave(请及时结贴) 及时回复。
我的意思不是这种,这种不是标准的文件夹选择对话框,在VC下就有标准的文件夹选择对话框,不知道Java下如何实现。
JFace下是可以实现标准的文件夹选择对话框。代码如下:
//Send questions, comments, bug reports, etc. to the authors:

//Rob Warner (rwarner@interspatial.com)
//Robert Harris (rbrt_harris@yahoo.com)

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
* This class demonstrates the DirectoryDialog class
*/
public class ShowDirectoryDialog {
/**
* Runs the application
*/
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Directory Browser");
createContents(shell);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

/**
* Creates the window contents
*
* @param shell the parent shell
*/
private void createContents(final Shell shell) {
shell.setLayout(new GridLayout(6, true));
new Label(shell, SWT.NONE).setText("Directory:");

// Create the text box extra wide to show long paths
final Text text = new Text(shell, SWT.BORDER);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 4;
text.setLayoutData(data);

// Clicking the button will allow the user
// to select a directory
Button button = new Button(shell, SWT.PUSH);
button.setText("Browse...");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
DirectoryDialog dlg = new DirectoryDialog(shell);

// Set the initial filter path according
// to anything they've selected or typed in
dlg.setFilterPath(text.getText());

// Change the title bar text
dlg.setText("SWT's DirectoryDialog");

// Customizable message displayed in the dialog
dlg.setMessage("Select a directory");

// Calling open() will open and run the dialog.
// It will return the selected directory, or
// null if user cancels
String dir = dlg.open();
if (dir != null) {
// Set the text box to the new selection
text.setText(dir);
}
}
});
}

/**
* The application entry point
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new ShowDirectoryDialog().run();
}
}

但是用Swing库怎么实现呢?
fool_leave 2006-12-21
  • 打赏
  • 举报
回复
JFrame f=new JFrame();
f.setVisible(true);
JFileChooser c=new JFileChooser();
c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
c.showSaveDialog(f);

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧