I'm trying to get a pop up as soon as a user logs in. I already have the pop up, but it keeps popping up all the results one at a time when I only need it to pop up within 15 minutes before the appointment time. How can I focus it to just do that?

Here is my code:

public Reminder() {

    String query = "SELECT contact, start FROM appointment WHERE createdBy = '" + MainApp.currentUser + "'";

    try {
    ps = connection.prepareStatement(query);
    rs = ps.executeQuery();

    while(rs.next()) {

        Timestamp tsStart = rs.getTimestamp("start");
        ZoneId newZid = ZoneId.systemDefault();
        ZonedDateTime newZdtStart = tsStart.toLocalDateTime().atZone(ZoneId.of("UTC"));
        ZonedDateTime newLocalStart = newZdtStart.withZoneSameInstant(newZid);

//      System.out.println(newLocalStart);
//            ChronoLocalDateTime<?> cldt = null;

//      if (rs.getTimestamp("start").toLocalDateTime().plusMinutes(15).isAfter(LocalDateTime.now())) {
//                if (rs.getTimestamp("start").toLocalDateTime().plusMinutes(15).isBefore(LocalDateTime.now())) {
                if(LocalDateTime.now().plusMinutes(15).isBefore(rs.getTimestamp("start").toLocalDateTime())) {

            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.initOwner(dialogStage);
            alert.setTitle("Appointment Schedule");
            alert.setHeaderText("You have an appointment!");
            alert.setContentText("Starts at " + newLocalStart + " with " + rs.getString("contact"));

            alert.showAndWait();
        }
    }
    ps.execute();
    ps.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
}

I'm trying to get a pop up as soon as a user logs in. I already have the pop up, but it keeps popping up all the results one at a time when I only need it to pop up within 15 minutes before the appointment time.

???
This seems to make no sense at all. Is the popup to do with a login, or an appointment? What are "all the results" for a log in, or an appointment time?
Maybe you can explain this more clearly.

Sorry about that, but it has to do with appointments. When the user first logs in, it gives the notification that there is an appointment at such and such time. With the code I have, it pops up the all appointments after the date in the database. I need it to be like Microsoft Outlook where you get the notification within 15 minutes before the scheduled appointment.

Hope that clears it up.

Yes, that's a lot clearer.
Here's a suggestion:
When the user logs on, retrieve all his appts.
For each appt work out how many seconds it is from now until 15 mins before appt time, then start a Timer that will display the notification after that time has elapsed

Simply modify your query.

"SELECT contact, start FROM appointment WHERE createdBy = ? and start >= ? and start <= ?"

// and use

ps.setString(1, MainApp.currentUser);
ps.setString(2, new Date());
ps.setString(3, new Date(new Date().getTime() + (1000 * 60 * 15)));

// 1000 millisecond * 60 seconds * 15 minutes = number of milliseconds in 15 minutes.

And then keep a list/map of appointments you have already displayed and check these against the appointments the query returns so that you don't show one you've already shown.

You would, of course, also have to regularly check, but I assume you are already doing that. The interval is something you may need to experiment with.

As a note, it would behoove you to create a primary key on the table so that you do not need to keep the entire appointment in memory, but rather just the key value (such as an auto-increment number). You can then also retrieve all information for an appointment at the time it is needed by querying for the specific key.

This is what I have come up with, but it still will not pop up

public Reminders() {

     Timeline test = new Timeline();

    String query_to_get_appoitments = "SELECT appointmentId, description, url,  UNIX_TIMESTAMP(start) - UNIX_TIMESTAMP() as timeLeft, contact FROM appointment WHERE createdBy = '" + MainApp.currentUser + "'";

    try {
        ps = connection.prepareStatement(query_to_get_appoitments);
        rs = ps.executeQuery();

        while (rs.next()) {

            if (rs.getLong("timeLeft") > 16 * 60) {
               System.out.println(rs.getLong("timeLeft") - 15*60);
               String contactName = rs.getString("contact");

               test.getKeyFrames().add(new KeyFrame(Duration.seconds(rs.getLong("timeLeft") - 15*60), new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("this is called every 5 seconds on UI thread");

        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("Time's up!");
        alert.setHeaderText("Attention");

                alert.setContentText("You have an scheduled apoitment in 15 minutes with " + contactName  );

        alert.showAndWait();

        }
    }));

            }
        }

        ps.close();
        rs.close();
    } catch (SQLException ex) {
        //Logger.getLogger(ReportsController.class.getName()).log(Level.SEVERE, null, ex);
    }

    test.play();

}

That looks very reasonable. Does the print on line 14 show the expected value?

Yes it does

and do you get the print from line 21?

ps: I'm not familiar with using KeyFrames as a countdown timer like that. Aren't KeyFrames run sequentially on the same TimeLine? Have you tried a simple traditional Timer?

No I do not

Then that sounds like your handle method is never being called. Try it with a traditional Timer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.