Program w tle

0

Witam,

Próbuję stworzyć prościutki program, który będzie działał w tle i co zadany interwał sprawdzał coś, i zależnie od wyniku pokazywał informację na ekranie komputera.
Tak jak to mam to w kodzie poniżej, to działa, ale nie do końca ... tzn. chciałbym żeby kliknięcie ok w komunikacie ropoczynało liczenie czasu od początku, bo teraz to działa tak, że uruchomię program, poczekam 5min i pokaże się jedno okno, kliknę OK, pokaże się od razu drugie .. i tak 20 razy ...

Może ktoś mi pokazać kierunek?

dzięki.

package komunikaty.my.project;

import ...

public class AgentSwing {

	public static void main(String[] args) {
		int delay = 5000; // delay for 5 sec.
		int period = 15000; // repeat every 15 sec.
		
		java.util.Timer timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask(){
			public void run()
			{
				doIt();		
			}
		}, delay, period);
	}

	public static void doIt() {
		String komunikat;
                //tu cos sprawdzam zeby uzyskac komunikat="jest komunikat" albo komunikat="";

		JFrame frame = new JFrame("");
	        if (komunikat.equals(""))
		  komunikat = "Brak komunikatu";
		  // nic nie wyswietla
		else {
		    komunikat = "Komunikat ... ";     					    
		    JOptionPane.showMessageDialog(frame, komunikat);
		};
	}

		
}
0

Niech razem z pokazaniem się okna będzie anulowany (cancel()) timertask. Następnie kliknięcie ok powoduje jego ponowne uruchomienie.

0

No właśnie :) Taki pomysł też miałem, ale realizacja już gorzej ...
tzn może zostać Timer ?

Przy próbach z timer.cancel() nie umiem zrobić aby "wrócił do życia",
jak daje timer.cancel() to po kliknięciu OK, program się kończy.

Może pomożesz fragmentem kodu?

dzięki

0
public class AgentSwing {

       int delay = 5000; // delay for 5 sec.
       int period = 15000; // repeat every 15 sec.
               
        java.util.Timer timer = new Timer();

        public static void main(String[] args) {
                timer.scheduleAtFixedRate(tt(), delay, period);
        }

        public static void doIt() {
                String komunikat;
                //tu cos sprawdzam zeby uzyskac komunikat="jest komunikat" albo komunikat="";

                JFrame frame = new JFrame("");
                if (komunikat.equals(""))
                  komunikat = "Brak komunikatu";
                  // nic nie wyswietla
                else {
                    komunikat = "Komunikat ... ";                                                 
                    JOptionPane.	showConfirmDialog(frame, komunikat, JOptionPane.YES_OPTION );

                    int x = timer.scheduleAtFixedRate(tt(), delay, period);
                    if(x>0){
timer.scheduleAtFixedRate(tt(), delay, period);
                    }
                };
        }

         private TimerTask tt(){
                new TimerTask(){
                        public void run()
                        {
                                doIt();               
                        }
         }
}
               
}

Co w tym stylu

0

Dzięki za pomoc, ale chyba moja znajomość Javy jest za słaba na takie przykłady ..

jak mam np rozumieć :
int x = timer.scheduleAtFixedRate(tt(), delay, period); ?

bo metoda scheduleAtFixedRate zwraca void ...

0

mój błąd :) powinno być:

int x = JOptionPane.showConfirmDialog(frame, komunikat, JOptionPane.YES_OPTION );
timer.scheduleAtFixedRate(tt(), delay, period);
0

A gdzie timer.cancel() ?

bo tak to chyba nic z tego nie będzie ... zadziała tak jak w pierwszym poście.

0

zapamiętać... nie używać funkcji kopiuj wklej na forum :)

package eu.runelord.programmers.t135469;

import java.awt.Component;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class AgentSwing {

	int delay = 1000; // delay for 5 sec.
	int period = 5000; // repeat every 15 sec.

	Timer timer = new Timer();
	
	String komunikat = "";


	public static void main(String[] args) {
		AgentSwing as = new AgentSwing();
		as.timer.scheduleAtFixedRate(as.tt(), as.delay, as.period);
	}

	public void doIt() {
		JFrame frame = new JFrame("");
		if (komunikat.equals("")) {
			komunikat = "Brak komunikatu";
		} else {
			komunikat = "Komunikat ... ";
			int x = JOptionPane.showConfirmDialog((Component) frame,
					(Object) komunikat, "", JOptionPane.YES_OPTION);

			timer.scheduleAtFixedRate(tt(), delay, period);
			if (x == 0) { // wybrano yes
				System.out.println(x);
				timer.scheduleAtFixedRate(tt(), delay, period);
				timer.cancel();
				komunikat = "";
			}
		}
	}

	private TimerTask tt() {
		return new TimerTask() {
			public void run() {
				doIt();
			}
		};
	}
}

0

Dzięki za pomoc, ale udało mi się to zrobić inaczej.
W sumie identycznie z ostatnim przykładem z :
http://gallemore.blogspot.com/2007/05/java-threading-scheduledexecutorservice.html

1 użytkowników online, w tym zalogowanych: 0, gości: 1