Makdumul Islam Makdumul Islam - 9 months ago 30
Java Question

in java how to show input dialog message for specific time?

In my program I want to show an input dialog box to get an input from the user. But if user doesn't give the feedback within a specific time the input dialog box will be close and the default setting will be run.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;


public class WorkerDemo {





public static void main(String[] args) {

JDialog.setDefaultLookAndFeelDecorated(true);
final JOptionPane Message = new JOptionPane("do u want to stop posting?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);


final JDialog getInput = Message.createDialog("Select Yes or No");
final int Option = Message.PROPERTIES;
getInput.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

Message.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
//super.componentShown(e);
final Timer t = new Timer(5000,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Message.setVisible(false);
}
});
t.start();
t.setRepeats(false);

}
});

Message.setVisible(true);

if (Option == JOptionPane.NO_OPTION) {
System.out.println("No button clicked");
} else if (Option == JOptionPane.YES_OPTION) {
System.out.println("Yes button clicked");
} else if (Option == JOptionPane.CLOSED_OPTION) {
System.out.println("JOptionPane closed");}

System.out.println("Outside code.");


}
}


here the problem is if condition is not working. is there any way to solve this problem. thanks in advance.

Answer Source

Here is a working version of your code:

import javax.swing.*;

public class WorkerDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JDialog.setDefaultLookAndFeelDecorated(true);
            JOptionPane optionPane = new JOptionPane("do u want to stop  posting?",
                                                     JOptionPane.QUESTION_MESSAGE,
                                                     JOptionPane.YES_NO_OPTION);


            JDialog dialog = optionPane.createDialog("Select Yes or No");
            dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

            Timer timer = new Timer(5000, e -> dialog.setVisible(false));
            timer.setRepeats(false);
            timer.start();

            dialog.setVisible(true);

            if (optionPane.getValue() instanceof Integer) {
                int option = (Integer) optionPane.getValue();

                if (option == JOptionPane.NO_OPTION) {
                    System.out.println("No button clicked");
                }
                else if (option == JOptionPane.YES_OPTION) {
                    System.out.println("Yes button clicked");
                }
            }
            else {
                System.out.println("dialog was closed by timer");
            }

            System.out.println("Outside code.");
        });
    }
}

Explanations and notes:

  • I chose better names for your variables, and respected the Java naming conventions
  • I made the dialog visible/invisible, not the option pane
  • I replaced inner classes by lambda expressions, to make the code shorter and more readable
  • The option is obtained from the optionPane's getValue() method, which returns what the user selected. Your way of initializing it with the constant Message.PROPERTIES makes no sense.
  • Everything executes, as it should, in the event dispatch thread, thanks to SwingUtilities.invoeLater().
  • The timer is simply start just before making the dialog visible. No need for a component listener to do that.
  • No need to handle CLOSED_OPTION, since you explicitly told the option pane to do nothing on close: this option is impossible.