Results 1 to 16 of 16
  1. #1
    GRPsuper9 is offline Member
    Join Date
    May 2015
    Posts
    16
    Rep Power
    0

    Default Multipule Message Boxes

    Hey guys,

    In vb.Net, when I do the code msgbox("hello world") and put it in a loop, it will execute that command and show a new messagebox for every time the code goes through the loop. I am wanting to do something similar with java but I'm afraid I don't know how.

    I know that the following code will display a messagebox as I am describing it
    Java Code:
    JOptionPane.showMessageDialog(null, "message", "title", 1);
    But when put in a loop, it does two things:
    1) It hangs the program
    2) It will only display the first message box and when you press ok, the other message boxes will be disregarded and not displayed (I'm assuming because of the hang).

    Could someone please help me solve this?

    Thanks!

  2. #2
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: Multipule Message Boxes

    as far as I know the JOptionPane isn't mean to show multiple windows
    it's meant to have only 1 active at a time

    so for example if your program needs an error message to show you use the JOptionPane, which you can only close by pressing 'Ok'
    you can't set focus to any background windows while the JOptionPane is open either.

    if you want more than one window to open I guess you need to set a loop to open more frames

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Multipule Message Boxes

    It hangs the program
    From the API doc:
    All dialogs are modal. Each showXxxDialog method blocks the caller until the user's interaction is complete.
    help me solve this
    What do you want the program to do?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    GRPsuper9 is offline Member
    Join Date
    May 2015
    Posts
    16
    Rep Power
    0

    Default Re: Multipule Message Boxes

    I know it is supposed to block the application because I've been coding in vb.net for years and this has always been the case as well.

    I am creating a reminder program and every time that the current time of the machine matches with the one that the user has inputted to be reminded at, I want a message box to display the reminder. Now lets say that 2 reminders have elapsed and the user didn't click okay to the first message, the second messagebox will not be displayed. I figured explaining this with loops would be easier.

    I want the program to be able to display another messagebox on top of an old one if the user hasn't acknowledged an old messagebox yet rather than disregarding the new messagebox.

    Java Code:
    for (int i = 0; i < 10; i++) {
        JOptionPane.showMessageDialog(null, i, "title", 1);
    }
    for instance, my intention for that code is for it to show a message 10 times, but in reality, it will only show the first message and when the user clicks ok, it will disregard all the other messages that followed. I want it to show all the messages.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Multipule Message Boxes

    that code is for it to show a message 10 times, but in reality, it will only show the first message and when the user clicks ok, it will disregard all the other messages that followed.
    For me: That code shows all the messages that the loop calls for (I reduced it to 4 for testing). It does not skip/disregard any of the messages.
    Is that the exact code you have compiled and executed for testing? It should show the message 10 times.

    the second messagebox will not be displayed.
    Is that because the program's logic misses long past due events?

    Use the Dialog class in non-modal in order to not block.
    Last edited by Norm; 06-04-2015 at 03:52 PM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    GRPsuper9 is offline Member
    Join Date
    May 2015
    Posts
    16
    Rep Power
    0

    Default Re: Multipule Message Boxes

    Quote Originally Posted by Norm View Post
    For me: That code shows all the messages that the loop calls for (I reduced it to 4 for testing). It does not skip/disregard any of the messages.
    Is that the exact code you have compiled and executed for testing? It should show the message 10 times.


    Is that because the program's logic misses long past due events?

    Use the Dialog class in non-modal in order to not block.
    Interesting. You're right. I just tested it inside a loop and it worked for me too. I guess I was wrong to assume that it wouldn't work.

    If I set 2 reminders at the same time, my program will actually display both, one after the other. But if I set 2 reminders a minute apart from one another, it will only display the first one and disregard the second. Attached is my code for the class.

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    import java.net.URL;
    import java.time.LocalDate;
    import java.util.Date;
    import java.util.ResourceBundle;
    import javafx.animation.KeyFrame;
    import javafx.animation.Timeline;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Button;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.DatePicker;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.control.TextArea;
    import javafx.scene.control.cell.PropertyValueFactory;
    import javafx.util.Duration;
    import javax.swing.JOptionPane;
    
    /**
     * FXML Controller class
     *
     * @author Gaston
     */
    public class MainController implements Initializable {
        @FXML
        private TableView<Reminder> tblReminders;
        @FXML
        private TableColumn<Reminder, String> colDate;
        @FXML
        private TableColumn<Reminder, String> colReminder;
        @FXML
        private Button btnAdd;
        @FXML
        private Button btnDelete;
        
        private ObservableList<Reminder> reminders = FXCollections.observableArrayList();
        @FXML
        private DatePicker dpDate;
        @FXML
        private ComboBox cmbHour;
        @FXML
        private ComboBox cmbMinute;
        @FXML
        private ComboBox cmbAMPM;
        @FXML
        private TextArea txtReminder;
            
        private Timeline timeline;
        
        
        /**
         * Initializes the controller class.
         */
        @Override
        public void initialize(URL url, ResourceBundle rb) {
                    
            colDate.setCellValueFactory(new PropertyValueFactory<>("date"));
            colReminder.setCellValueFactory(new PropertyValueFactory<>("reminder"));
            
            tblReminders.setItems(reminders);
    
            String strNum;
            for (int i = 0; i <= 59; i++) {
                if (i < 10) {
                    strNum = "0" + i;
                } else {
                    strNum = String.valueOf(i);
                }
                
                if (i <= 12 && i >= 1) { //add the hours
                    cmbHour.getItems().add(strNum);  
                }
              
                cmbMinute.getItems().add(strNum); //add the minutes
            }
                    
            cmbAMPM.getItems().addAll("AM", "PM"); //set the AM or PM
            
            
            dpDate.setValue(LocalDate.now());
            
            timeline = new Timeline(new KeyFrame( 
                    Duration.millis(1000),
                    ae -> checkReminders()));
            timeline.setCycleCount(Timeline.INDEFINITE);
            timeline.play();
        }    
        
        private void checkReminders() { //method to check if reminders are up
            if (reminders.isEmpty() == false) { //only check the reminders if there are any reminders inside the list
                Date date = new Date(); //make the variable date be the current date and time
                for (Reminder reminder : reminders) { //check each reminder in reminders
                    if (date.getTime() >= Date.parse(reminder.getDate()) - 1000 && date.getTime() <= Date.parse(reminder.getDate())) { //compare the reminder's date (which the user entered) to the date variable we just created
                        JOptionPane.showMessageDialog(null, reminder, "Reminder!", JOptionPane.INFORMATION_MESSAGE); //if they are the same or within 1 second of each other, display this messagebox
                    }
                }
            }
        }
    
        @FXML
        private void btnAddClick(ActionEvent event) {  
            boolean noErrors;
            
            //change minute
            String minute = cmbMinute.getValue().toString();
            if (Integer.parseInt(minute) < 10) {
                minute = "0" + minute;
            }
            
            //change hour
            String hour = cmbHour.getValue().toString();
            if (Integer.parseInt(hour) < 10) {
                hour = "0" + hour;
            }
            
            //error checking
            if (Integer.parseInt(hour) >= 1 && Integer.parseInt(hour) <= 12 && Integer.parseInt(minute) >= 0 && Integer.parseInt(minute) <= 59) { //if numbers are correct
                noErrors = "AM".equals(cmbAMPM.getValue().toString().toUpperCase()) || "PM".equals(cmbAMPM.getValue().toString().toUpperCase());
            } else {
                noErrors = false;
            }
            
            if (noErrors) {
                reminders.add(new Reminder(dpDate.getValue() + " " + hour + ":" + minute + ":00 " + cmbAMPM.getValue().toString().toUpperCase(), txtReminder.getText()));
                cmbHour.setValue(""); cmbMinute.setValue(""); cmbAMPM.setValue(""); txtReminder.clear(); //reset all the values
            }
        }
    
        @FXML
        private void btnDeleteClick(ActionEvent event) {
            reminders.remove(tblReminders.getSelectionModel().getSelectedItem());
        }
        
    }
    Last edited by GRPsuper9; 06-04-2015 at 05:26 PM. Reason: added some comments to my code

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Multipule Message Boxes

    if I set 2 reminders a minute apart from one another, it will only display the first one and disregard the second.
    Describe the logic in English for checking if the time for a reminder has passed.

    I don't see any comments in the code saying where it is checking if the time for a reminder has passed.
    Add some comments to the code describing what the code is doing to check for the time of a reminder.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Multipule Message Boxes

    Just to note: I see both JavaFX and Swing are being mixed here. JavaFX has its own ways of showing modal dialogs:

    JavaFX Dialogs (official) | code.makery.ch
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    GRPsuper9 is offline Member
    Join Date
    May 2015
    Posts
    16
    Rep Power
    0

    Default Re: Multipule Message Boxes

    Quote Originally Posted by Norm View Post
    Describe the logic in English for checking if the time for a reminder has passed.

    I don't see any comments in the code saying where it is checking if the time for a reminder has passed.
    Add some comments to the code describing what the code is doing to check for the time of a reminder.
    I added some comments to my code inside the checkReminders method.

    Gimbal2, I actually tried using the Alert class, but netbeans wouldn't recognize it and would give me an error not allowing me to use it. I wonder if they took it out of Java 8.45 since the website says 8.40. Unless I am not doing it right. Would you be able to give me a working example that you have just done using that class so I can copy and paste it into my IDE and see what it says/works?
    Last edited by GRPsuper9; 06-04-2015 at 05:25 PM.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Multipule Message Boxes

    No sorry, I have no examples. Funny that you correctly spot this was added only very recently actually, apparently nowadays it is no longer a no-no to change the API in minor version updates.

    I updated to JDK 8 u45 and indeed cannot use Eclipse's "fix imports" to make it understand the Alert class exists. But when I directly use javafx.scene.control.Alert, the code compiles. I'm thinking the API change in a minor update throws off the IDEs as well.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Multipule Message Boxes

    //if they are the same or within 1 second of each other, display this messagebox
    That says that if the user misses OK on the Dialog for longer than 1 sec past a reminder, that reminder will be skipped.
    Is that what is happening? If not, please show a print out for an example of the problem and explain what is happening that you want to change.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    GRPsuper9 is offline Member
    Join Date
    May 2015
    Posts
    16
    Rep Power
    0

    Default Re: Multipule Message Boxes

    Quote Originally Posted by Norm View Post
    That says that if the user misses OK on the Dialog for longer than 1 sec past a reminder, that reminder will be skipped.
    Is that what is happening? If not, please show a print out for an example of the problem and explain what is happening that you want to change.
    When a user enters a time to be reminded, the user only has the option of entering the hour and minute. The seconds get automatically inputted as 00. When a user runs my program, the user may not run it exactly at the 00 second mark but rather 00.40 milliseconds. My program is running the timer from when the user opens the program and is running it at 1 second intervals from that time. Meaning if the user opened it at 1:30:41.40 PM, the program would then increment the seconds like so.... 42.40, 43.40, etc. As you notice, this isn't 42.00, 43.00 which my program checks for. This is why the if statement before that line is there, to check if the time has hit within 1000 milliseconds. No reminders are skipped because of this if statement since it covers the entire second leading up to the minute that the user has set.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Multipule Message Boxes

    No reminders are skipped
    Ok, remind me what the problem is.
    I thought that was what the problem was: skipped reminders.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    GRPsuper9 is offline Member
    Join Date
    May 2015
    Posts
    16
    Rep Power
    0

    Default Re: Multipule Message Boxes

    Quote Originally Posted by GRPsuper9 View Post
    I want the program to be able to display another messagebox on top of an old one if the user hasn't acknowledged an old messagebox yet rather than disregarding the new messagebox. For instance, my intention for that code is for it to show a message 10 times, but in reality, it will only show the first message and when the user clicks ok, it will disregard all the other messages that followed. I want it to show all the messages.
    Forget about what I said about the loop because obviously that wasn't right. The problem isn't that it is skipping reminders. The problem is that JOptionPane hangs on the first messagebox and then if time goes by, doesn't display the ones following. The logic to display the ones following is set properly. If you do System.out.println, the program will output correctly.

  15. #15
    GRPsuper9 is offline Member
    Join Date
    May 2015
    Posts
    16
    Rep Power
    0

    Default Re: Multipule Message Boxes

    here is a sample output with System.out.println.
    Java Code:
    run-single:
    2015/06/04 02:18:00 PM - hello world 1
    2015/06/04 02:18:00 PM - hello world 2
    2015/06/04 02:19:00 PM - hello world 3
    Here I set a reminder for 2:18:00PM to tell me hello world 1, 2:18:00pm tell me hello world 2, and 2:19:00pm hello world 3. My code functions correctly with system out. The way that it will work with JOptionPane would be that it would display hello world 1, and hello world 2, and when it gets to 2:19:00pm, it will not display that.
    Last edited by GRPsuper9; 06-04-2015 at 09:21 PM.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Multipule Message Boxes

    The problem is that JOptionPane hangs on the first messagebox
    Yes. The API doc says JOptionPane blocks until the user responds. See posts #3 and #5

    Use a modeless Dialog class window if you don't want the thread to be blocked.
    Or start a new Thread to use JOPtionPane so it won't block the thread with the search logic.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. add message to growl message componnent in primefaces
    By andree2014 in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 06-06-2014, 05:12 PM
  2. how to share same object to multipule threads ?
    By reddi.java97 in forum Advanced Java
    Replies: 2
    Last Post: 09-11-2013, 09:21 PM
  3. Replies: 7
    Last Post: 01-16-2012, 03:37 AM
  4. Replies: 6
    Last Post: 06-04-2010, 08:43 PM
  5. Built-in User Popup message boxes ?
    By BobZ in forum AWT / Swing
    Replies: 2
    Last Post: 02-06-2009, 06:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •