Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Small problem while using JOptionPane

Tags:

java

swing

I'm using JOptionPane.showMessageDialog(null,e,"Invalid Name",JOptionPane.ERROR_MESSAGE) method to display the exception extended from Exception class. But the Pop window is not getting displayed unless and until I press Alt+tab. What can be the reason? Below is the 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)
}
like image 731
Supereme Avatar asked Jan 23 '10 18:01

Supereme


People also ask

How do I get output from JOptionPane?

message =JOptionPane. showInputDialog("Enter a line of text:"); // Display the message. The interactive session consists of two windows, the first getting the message and the second one displaying the message.

How do you make JOptionPane always on top?

JOptionPane optionPane = new JOptionPane(); JDialog dialog = optionPane. createDialog("Title"); dialog. setAlwaysOnTop(alwaysOnTop); dialog. setVisible(true);

What is null in JOptionPane showMessageDialog?

Passing null to it just indicates that there's not an associated "parent" dialog - ie, the dialog being displayed does not belong to another dialog. Instead, you can use the overloaded signature and call it like this: showInputDialog(Object message)


3 Answers

I had the same behaviour on my machine. The trick is that you have to tell the JDialog class to set itself always on top - which is not possible with using the convenient static showMessageDialog method. So we have to create the JOptionPane and the JDialog by hand. Just add another static method to the ValidName class:

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);
}

and call this method instead of the JOptionPane.showMessageDialog. It works on my machine, the error message appears where it should: on top of my eclipse IDE.

like image 55
Andreas Dolk Avatar answered Nov 07 '22 22:11

Andreas Dolk


Most likely cause for this problem is that you are passing a null reference in the call to showMessageDialog. Though passing null as first parameter is fine it is likely to cause issues like the one you are facing. (Also I think you are facing this issue on Windows).

To fix this issue (if the above chunk of code is part of your larger GUI code base) pass the parent JFrame reference (instead of null) in your call to showMessageDiaglog.

Also check the article Finding Lost Frames of Java Specialist's news letter which has some more details about this kind of issue.

like image 22
sateesh Avatar answered Nov 07 '22 21:11

sateesh


While it is somewhat puzzling that you want to display a JOptionPane from a console program without further gui, the code seems to work fine.

I pasted it into my IDE, let it run and it correctly showed the option panes on corresponding input. I tried running it from a command prompt and the result was the same - the code runs fine without the problems you describe.

I suggest trying to run it from a command prompt yourself.

like image 2
Mathias Weyel Avatar answered Nov 07 '22 20:11

Mathias Weyel