Java中的JFileChooser没有提供文件夹选择的功能,而我想要的是文件夹目录选择的对话框,请问哪位DX能帮帮忙?跪谢。

解决方案 »

  1.   

    JFrame f=new JFrame();
       f.setVisible(true);
       JFileChooser c=new JFileChooser();
       c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
       c.showSaveDialog(f);
      

  2.   

    谢谢fool_leave(请及时结贴) 及时回复。
    我的意思不是这种,这种不是标准的文件夹选择对话框,在VC下就有标准的文件夹选择对话框,不知道Java下如何实现。
    JFace下是可以实现标准的文件夹选择对话框。代码如下:
    //Send questions, comments, bug reports, etc. to the authors://Rob Warner ([email protected])
    //Robert Harris ([email protected])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库怎么实现呢?
      

  3.   

    Swing库的文件夹选择只有一个JFileChooser
    除非你自己用Swing组件拼装
      

  4.   

    楼上的也是一种方法,l2fprod中有个控件就叫JDirectoryChooser,可以实现楼主需要的效果;不过我觉得用jfilechooser,设定DIRECTORIES_ONLY模式,效果是一样的