从选择侦听器java中获取数据

时间:2013-10-02 11:17:26

标签: java swt listener

我正在构建一个图形用户界面,并使用FileChooser来获取用户选择的文件,所以一切正常,但现在我正在尝试选择文件的路径(非常容易通过file.getAbsolutePath())。

但不知怎的,我无法将其从课堂中解脱出来...我希望在类中使用侦听器的String路径,如下所示:

private void browseButton(Canvas BasicSelection)
{
    final Button btnBrowse = new Button(BasicSelection, SWT.NONE);
    btnBrowse.setBounds(70, 29, 68, 23);
    btnBrowse.setText("Browse");
    btnBrowse.addSelectionListener(new SelectionAdapter() 
    {
        public void widgetSelected(SelectionEvent arg0) 
        {
            filechooser.createAndShowGUI(path);
            sbtnBrowse = btnBrowse.getSelection();
        }
    });
    launchEvent();
}

以下是单击按钮时执行的操作:

public void actionPerformed(ActionEvent e) 
{
    //Set up the file chooser.
    if (fc == null)
    {
        fc = new JFileChooser();
    }
    fc.addChoosableFileFilter(new Filter());
    fc.setAcceptAllFileFilterUsed(false);

    //Show it.
    int returnVal = fc.showDialog(FileChooser.this,"Attach");

    //Process the results.
    if (returnVal == JFileChooser.APPROVE_OPTION) 
    {
        File file = fc.getSelectedFile();
        setPath(file);
        log.append("Attaching file: " + file.getName() + "." + newline);
        // Here is where i would need to get the file... but how ?
    } 
    else 
    {
        log.append("Attachment cancelled by user." + newline);
    }
    log.setCaretPosition(log.getDocument().getLength());

    //Reset the file chooser for the next time it's shown.
    fc.setSelectedFile(null);
}

如何从课堂上获取文件名?

编辑:我已经尝试过使用getter和setter,但奇怪的是他试图在我实际写入之前访问内容。

String path ;
/*
 Rest of the code ...
*/
public void setPath(File input)
{
    if (input != null)
    {
        this.path = input.getAbsolutePath();
        System.out.println("path is now set correctly : ");
    }
}
public String getPath()
{
    return this.path;
}

2 个答案:

答案 0 :(得分:2)

这是一个非常简单的例子,应该完全符合您的要求:

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Select file");

    button.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            /* Create the dialog */
            FileDialog dialog = new FileDialog(shell, SWT.OPEN);

            /* Open it. The absolute path of the selected file will be saved in the String variable */
            String selection = dialog.open();

            /* If the user selected something, print it */
            if(selection != null)
                System.out.println(selection);
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

答案 1 :(得分:1)

执行与JFileChooser本身相同的操作 - 声明实例变量private String fileChosen(在类中,在任何方法之外),使用获得的路径设置它,并在此类上调用方法获取它(getSelectedFile()JFileChooser)执行此操作。