Little problem when using JOptionPane

I am using the JOptionPane.showMessageDialog (null, e, "Invalid Name", JOptionPane.ERROR_MESSAGE) method to display an exception extended from the Exception class. But the Pop window is not displayed until I press Alt + tab. What is the reason? Below is a snippet. Suggest me something.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;

class NameInvalidException extends Exception {
    /**
     * Invalid Name
     */
    String invName;

    public NameInvalidException() {
        super();
    }

    public NameInvalidException(String s) {
        invName = s;
    }

}

class SmallException extends Exception {
    /**
     * Short Name
     */
    String sName;

    public SmallException() {
        super();
    }

    public SmallException(String s) {
        sName = s;
    }

}

public class ValidName {
    public static void main(String arr[]) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    System.in));
            System.out.println("Enter the name: ");

            String name = br.readLine();
            checkName(name);
        } catch (IOException e) {
            System.out.println(e);
        }

    }// end main

    static void checkName(String name) {

        try {

            String sarr[] = name.split(" ");
            if (sarr.length != 3)
                throw new SmallException(name);
            for (int j = 0; j < 3; j++) {
                System.out.println("in j loop");
                if (sarr[j].length() < 3) {
                    throw new SmallException();
                }
            }
            for (int i = 0; i < name.length(); i++) {

                char ch = name.charAt(i);
                if (Character.isLetter(ch) || Character.isWhitespace(ch))
                    System.out.println("ok " + ch);
                else
                    throw new NameInvalidException();

            }// end for
        }// end try
        catch (NameInvalidException e) {
            JOptionPane.showMessageDialog(null, e.toString(), "Invalid Name",
                    JOptionPane.ERROR_MESSAGE);
            System.out.println(e);
        } catch (SmallException es) {
            JOptionPane.showMessageDialog(null, es.toString(), "Invalid Name",
                    JOptionPane.ERROR_MESSAGE);

        }
    }// end checkName(name)
}
+5
source share
3 answers

. , JDialog, - showMessageDialog. JOptionPane JDialog . ValidName:

private static void showErrorPane(String message, String title) {
   JOptionPane pane = new JOptionPane(message, JOptionPane.ERROR_MESSAGE);
   JDialog dialog = pane.createDialog(title);
   dialog.setAlwaysOnTop(true);
   dialog.setVisible(true);
}

JOptionPane.showMessageDialog. , , : Eclipse IDE.

+7

, , null showMessageDialog. null , , , . ( , Windows).

( ), JFrame ( null) showMessageDiaglog.

" " Java, .

+3

, JOptionPane gui, , , .

I put it in my IDE, let it run, and it correctly showed the parameter bar on the corresponding input. I tried to run it from the command line, and the result was the same - the code works fine without the problems that you describe.

I suggest trying to run it from the command line yourself.

+2
source

All Articles