java - Reminder using JOptionPane -
i designing 1 reminder type application using java swing.
in giving 2 options user, using joptionpane yes_no_option. want if user not given input within 10 minutes should choose yes default option.
what have that?
please check below code...
public void snooze() { int action = joptionpane.showconfirmdialog(null, "yes = close citrix , aventail \n no = snooze after 30min", "close", joptionpane.yes_no_option); if(action == 0){ killcitrix(); endaventail(); } else{ joptionpane.showmessagedialog(null, "will snooze after 30min"); try { timeunit.seconds.sleep(30); } catch (interruptedexception e2) { // todo auto-generated catch block e2.printstacktrace(); } snooze(); } }
as user1803551 mentioned, use timer
. here example:
- after 10 seconds of inactivity (if user didn't press button), dialog automatically dispose , selected option
joptionpane.yes_option
- if user pressed 'no', new
joptionpane
same functionalities appear after 5 seconds. - preview
code:
import java.awt.borderlayout; import java.awt.component; import java.awt.eventqueue; import java.awt.flowlayout; import java.awt.gridlayout; import java.awt.window; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mousewheelevent; import java.awt.event.mousewheellistener; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jcomponent; import javax.swing.jdialog; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtextarea; import javax.swing.timer; import javax.swing.border.titledborder; public class example { private int choice; private jtextarea log; public example() { log = new jtextarea(); log.seteditable(false); jframe frame = new jframe(); frame.add(log); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(800, 600); frame.setlocationrelativeto(null); frame.setvisible(true); showconfirmdialog(frame); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { new example(); } }); } public void showconfirmdialog(component parent) { timer timer = new timer(0, null); timer timer2 = new timer(0, null); log.append("will automatically press 'yes' after 10 seconds.\n"); timer.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { (window window : window.getwindows()) { if (window instanceof jdialog) { jdialog dialog = (jdialog) window; if (dialog.getcontentpane().getcomponentcount() == 1 && dialog.getcontentpane().getcomponent(0) instanceof joptionpane && dialog.gettitle().equals("dialog")) { dialog.dispose(); choice = joptionpane.yes_option; log.append("programmatically pressed 'yes' on joptionpane due inactivity of user\n"); } } } timer.stop(); timer2.stop(); } }); timer.setinitialdelay(10 * 1000); timer.start(); choice = joptionpane.showconfirmdialog(parent, "", "dialog", joptionpane.yes_no_cancel_option); if (choice == joptionpane.no_option) { log.append("dialog reappear in 5 seconds\n"); timer2.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { log.append("dialog reappeared\n"); showconfirmdialog(parent); timer2.stop(); } }); timer2.setinitialdelay(5 * 1000); timer2.start(); } timer.stop(); } }
Comments
Post a Comment