在Windows服务上运行时无法使用JFileChooser显示对话框

时间:2017-01-25 04:38:55

标签: java windows-services jfilechooser

我使用JFileChooser通过我的服务提供浏览功能。 当我通过命令提示符运行服务时,代码运行正常。每当我将服务作为Windows服务运行时,浏览稀释盒都不会出现。

以下是我实现上述目标的代码:

try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) { }

        JFrame frame = new JFrame();
        frame.add(new JScrollPane(null), BorderLayout.CENTER);
        frame.setAlwaysOnTop(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(true);
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle(title);
        chooser.setMultiSelectionEnabled(true);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        chooser.setApproveButtonText("Select");
        LOGGER.info("getBrowsedData: before showOpenDialog");
        //chooser.setInheritsPopupMenu(true);
        int temp = chooser.showOpenDialog(new JPanel());
        LOGGER.info("getBrowsedData: after showOpenDialog, temp: " + temp);
        //if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
        if (temp == JFileChooser.APPROVE_OPTION) {
            LOGGER.debug("getCurrentDirectory(): " + chooser.getCurrentDirectory());
              //File[] uploadDir = chooser.getSelectedFiles();
              List<String> browsedFolders = new ArrayList<>();
              for(File item: chooser.getSelectedFiles()) {
                  LOGGER.debug("folder name: " + item.getAbsolutePath());
                  browsedFolders.add(item.getAbsolutePath());
              }
              return browsedFolders;
        } else {
            LOGGER.info("getBrowsedData: in else showOpenDialog, temp: " + temp);
            LOGGER.error("No Selection for " + title);
        }

0 个答案:

没有答案