开发者

FileChooserBuilder not displayed in center of screen

I'm currently trying to use FileChooserBuilder from Netbeans Platform API. Following code is complete netbeans module action. When run, it doesn't show at the center of window/screen but somewhere in bottom left corner of the screen. Is there any possibility to make this dialog display in the middle of the screen?

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; // no开发者_运维知识库thing to do
        }

        // do some processing here
    }

}

Thanks for your ideas.


Found the solution:

You have to obtain JFileChooser instance and set right parent component in it's showOpenDialog method (it's then positioned relatively to application's main window). But as NetBeans tries to work quite safely with the threads - it allows only one thread access the components, so the EventQueue.invokeLater has to be used.

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
            }
        });

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜