在循环内处理多个JavaFX应用程序启动

时间:2015-12-08 00:37:43

标签: javafx controller javamail stage actionevent

我的代码目前通过IMAP(imaps)和javamail读取我的Gmail收件箱,一旦找到带有zip / xap附件的电子邮件,它会显示一个阶段(窗口)询问是否下载该文件,是或否。

我希望在进行选择后关闭舞台,然后返回到来自呼叫的循环内的位置。我的问题出现是因为您无法多次启动应用程序,因此我在Platform.setImplicitExit(false);方法中写了start,然后使用primartyStage.hide()(?)当我需要稍后再次显示舞台时,我会像Platform.runLater(() -> primaryStage.show());那样。

现在出现的问题是命令流在Mail.java的doit()方法中开始,该方法在我的收件箱中循环,而launch(args)发生在for loop内。方法。这意味着launch(args)然后调用start来设置场景,并显示舞台。由于存在Controller.javafxml关联,因此Controller类具有舞台按钮的事件处理程序,其中"拦截" start显示阶段后的流程。因此,当我单击是或否时,它会隐藏舞台,但只有挂起。好像它无法返回到start方法继续发生启动(args)的循环。如何在必要时正确隐藏/显示舞台,无论是否点击都允许循环继续。

这是Mail.java和Controller.java的代码。非常感谢!

Mail.java

 [Other variables set here]
    public static int launchCount = 0;@FXML public Text subjectHolder;

    public static ReceiveMailImap obj = new ReceiveMailImap();

    public static void main(String[] args) throws IOException,        MessagingException {
        ReceiveMailImap.doit();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        loader = new FXMLLoader(getClass().getResource("prompts.fxml"));
        root = loader.load();
        controller = loader.getController();
        controller.setPrimaryStage(primaryStage);
        scene = new Scene(root, 450, 250);
        controller.setPrimaryScene(scene);
        scene.getStylesheets().add("styleMain.css");
        Platform.setImplicitExit(false);
        primaryStage.setTitle("Download this file?");
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void doit() throws MessagingException, IOException {
        Folder inbox = null;
        Store store = null;

        try {
            Properties props = System.getProperties();
            Session session = Session.getDefaultInstance(props, null);
            store = session.getStore("imaps");
            store.connect("imap.gmail.com", "myAccount@gmail.com", "Password");
            inbox = store.getFolder("Inbox");
            inbox.open(Folder.READ_WRITE);
            Message[] messages = inbox.getMessages();
            FetchProfile fp = new FetchProfile();
            fp.add(FetchProfile.Item.ENVELOPE);
            fp.add(UIDFolder.FetchProfileItem.FLAGS);
            fp.add(UIDFolder.FetchProfileItem.CONTENT_INFO);
            fp.add("X-mailer");

            inbox.fetch(messages, fp);
            int doc = 0;
            int maxDocs = 400;
            for (int i = messages.length - 1; i >= 0; i--) {
                Message message = messages[i];
                if (doc < maxDocs) {
                    doc++;
                    message.getSubject();

                    if (!hasAttachments(message)) {
                        continue;
                    }
                    String from = "Sender Unknown";
                    if (message.getReplyTo().length >= 1) {
                        from = message.getReplyTo()[0].toString();
                    } else if (message.getFrom().length >= 1) {
                        from = message.getFrom()[0].toString();
                    }
                    subject = message.getSubject();
                    if (from.contains("myAccount@gmail.com")) {
                        saveAttachment(message.getContent());
                        message.setFlag(Flags.Flag.SEEN, true);
                    }
                }
            }
        } finally {
            if (inbox != null) {
                inbox.close(true);
            }
            if (store != null) {
                store.close();
            }
        }
    }

    public static boolean hasAttachments(Message msg) throws MessagingException, IOException {
        if (msg.isMimeType("multipart/mixed")) {
            Multipart mp = (Multipart) msg.getContent();
            if (mp.getCount() > 1) return true;
        }
        return false;
    }

    public static void saveAttachment(Object content)
    throws IOException, MessagingException {
        out = null; in = null;
        try {
            if (content instanceof Multipart) {
                Multipart multi = ((Multipart) content);
                parts = multi.getCount();
                for (int j = 0; j < parts; ++j) {
                    part = (MimeBodyPart) multi.getBodyPart(j);
                    if (part.getContent() instanceof Multipart) {
                        // part-within-a-part, do some recursion...
                        saveAttachment(part.getContent());
                    } else {
                        int allow = 0;
                        if (part.isMimeType("application/x-silverlight-app")) {
                            extension = "xap";
                            allow = 1;
                        } else {
                            extension = "zip";
                            allow = 1;
                        }
                        if (allow == 1) {
                            if (launchCount == 0) {
                                launch(args);
                                launchCount++;
                            } else {
                                Platform.runLater(() -> primaryStage.show());
                            }
                        } else {
                            continue;
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if ( in != null) { in .close();
            }
            if (out != null) {
                out.flush();
                out.close();
            }
        }
    }

    public static File createFolder(String subject) {
        JFileChooser fr = new JFileChooser();
        FileSystemView myDocs = fr.getFileSystemView();
        String myDocuments = myDocs.getDefaultDirectory().toString();
        dir = new File(myDocuments + "\\" + subject);
        savePathNoExtension = dir.toString();
        dir.mkdir();
        System.out.println("Just created: " + dir);
        return dir;
    }

}

Controller.java

public class Controller implements Initializable {

    @FXML
    private Text subjectHolder;
    public Button yesButton, noButton;
    public ReceiveMailImap subject;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        subject= new ReceiveMailImap();
        subjectHolder.setText(subject.returnSubject());
    }
    public Stage primaryStage;
    public Scene scene;

    @FXML
    ComboBox<String> fieldCombo;

    public void setPrimaryStage(Stage stage) {
        this.primaryStage = stage;
    }

    public void setPrimaryScene(Scene scene) {
        this.scene = scene;
    }

    public String buttonPressed(ActionEvent e) throws IOException, MessagingException {
        Object source = e.getSource();
        if(source==yesButton){
            System.out.println("How to tell Mail.java that user clicked Yes?");
            return "POSITIVE";}
        else{subject.dlOrNot("no");
            System.out.println("How to tell Mail.java that user clicked No?");
            primaryStage.hide();
            return "NEGATIVE";}
    }

}

1 个答案:

答案 0 :(得分:2)

您发布的代码存在很多问题,但我只想尝试解决您提出的问题。

代码挂起的原因是Application.launch(...)

  

在应用程序退出之前不会返回

通常,您在这里误解了JavaFX应用程序的整个生命周期。您应该将start(...)方法视为&#34;传统&#34;中main(...)方法的等效方法。 Java应用程序。唯一需要注意的是start(...)是在FX应用程序线程上执行的,所以如果你需要执行任何阻塞代码,你需要将它放在后台线程中。

为方便起见,start(...)方法传递了Stage实例,因为最常见的做法是创建场景图并在舞台中显示它。您没有义务使用此阶段,您可以忽略它,只需在您需要时创建自己的阶段。

我认为您基本上可以按如下方式构建代码(但是,老实说,我在理解您正在做的事情上遇到很多麻烦):

public class Mail extends Application {

    @Override
    public void start(Stage ignored) throws Exception {

        Platform.setImplicitExit(false);

        Message[] messages = /* retrieve messages */ ;
        for (Message message : messages) {
            if ( /* need to display window */) {
                showMessage(message);
            }
        }
    }

    private void showMessage(Message message) {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("prompts.fxml"));
        Parent root = loader.load();

        Controller controller = loader.getController();
        Scene scene = new Scene(root, 450, 250);
        stage.setScene(scene);
        stage.initStyle(StageStyle.UNDECORATED);
        stage.setTitle(...);

        // showAndWait will block execution until the window is hidden, so 
        // you can query which button was pressed afterwards:

        stage.showAndWait();

        if (controller.wasYesPressed()) {
            // ...
        }
    }

    // for IDEs that don't support directly launching a JavaFX Application:
    public static void main(String[] args) { 
        launch(args);
    }
}

显然,您决定是否显示窗口的逻辑更为复杂,但这将为您提供基本结构。

要检查按下了哪个按钮,请使用上面的showAndWait然后在控制器中执行

public class Controller {

    @FXML
    private Button yesButton ;

    private boolean yesButtonPressed = false ;

    public boolean wasYesPressed() {
        return yesButtonPressed ;
    }

    // use different handlers for different buttons:
    @FXML
    private void yesButtonPressed() {
        yesButtonPressed = true ;
        closeWindow();
    }

    @FXML
    private void noButtonPressed() {
        yesButtonPressed = false ; // not really needed, but makes things clearer
        closeWindow();
    }

    private void closeWindow() {
        // can use any @FXML-injected node here:
        yesButton.getScene().getWindow().hide();
    }
}