FileChooserBuilder未显示在屏幕中央

时间:2011-04-08 13:50:39

标签: java netbeans-platform

我目前正在尝试使用Netbeans Platform API中的FileChooserBuilder。以下代码完成netbeans模块操作。运行时,它不会显示在窗口/屏幕的中央,而是显示在屏幕左下角的某个位置。是否有可能在屏幕中间显示此对话框?

public final class LoadProjectAction implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        File home = new File(
            System.getProperty("user.home")
            + File.separator + "lib");

        FileChooserBuilder fileChooserBuilder = new FileChooserBuilder(
            LoadProjectAction.class);
        fileChooserBuilder.setTitle("Load project");
        fileChooserBuilder.setDefaultWorkingDirectory(home);
        fileChooserBuilder.setApproveText("Load");
        fileChooserBuilder.setDirectoriesOnly(true);


        File directory = fileChooserBuilder.showOpenDialog();

        if (directory != null) {
            return; // nothing to do
        }

        // do some processing here
    }

}

感谢您的想法。

1 个答案:

答案 0 :(得分:0)

找到解决方案:

你必须获得JFileChooser实例并在其showOpenDialog方法中设置右父组件(然后它相对于应用程序的主窗口定位)。但是当NetBeans尝试使用线程安全地工作时 - 它只允许一个线程访问组件,因此必须使用EventQueue.invokeLater。

public final class LoadProjectAction implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                // output window
                InputOutput io = IOProvider.getDefault().getIO("File search", true);
                io.select();

                // start in user home directory
                File initialDirectory = new File(
                    System.getProperty("user.home")
                    + File.separator + "lib");

                FileChooserBuilder fileChooserBuilder = new FileChooserBuilder(
                    "LoadProjectAction");
                fileChooserBuilder.setTitle("Load project");
                fileChooserBuilder.setDefaultWorkingDirectory(initialDirectory);
                fileChooserBuilder.setApproveText("Load");
                fileChooserBuilder.setDirectoriesOnly(true);

                JFileChooser jfc = fileChooserBuilder.createFileChooser();
                int value = jfc.showOpenDialog(WindowManager.getDefault().getMainWindow());

                if (value != JFileChooser.APPROVE_OPTION) {
                    return; // nothing to do
                }

                // process selection
            }
        });

    }
}