Raghuram Orsu February 2016

Reminder using JOptionPane

I am designing one reminder type application using java swing.

In this I am giving two options to user, using JOptionPane YES_NO_OPTION. But what i want is if user not given any input within 10 minutes it should choose YES as default option.

what i have to do for that?

Please check below code...

public void snooze()
{
    int action = JOptionPane.showConfirmDialog(null, "Yes = close citrix and 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();
    }
}

Answers


LuxxMiner February 2016

As user1803551 already mentioned, use a Timer. Here is an example:

  1. after 10 seconds of inactivity (if the user didn't press any button), the dialog will automatically dispose and the selected option will be JOptionPane.YES_OPTION
  2. If the user pressed 'No', a new JOptionPane with the same functionalities will appear after 5 seconds.
  3. 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 p 

Post Status

Asked in February 2016
Viewed 3,265 times
Voted 4
Answered 1 times

Search




Leave an answer