0

He is my code:

    import java.util.Scanner;
    import javax.swing.JOptionPane;
    public class TicketNumber 
    {
        public static void main(String[] args) 
        {
            Scanner key = new Scanner(System.in);
            System.out.print("Please enter your six digit ticket number: ");
                int tNumber = key.nextInt();
                int lastDigit = tNumber%10;
                tNumber = (tNumber -(lastDigit))/10;
                boolean evaluation = false;
            if (tNumber%7 == lastDigit)
                evaluation = true;
            JOptionPane.showMessageDialog(null, "This number is a " + evaluation + " ticket number.");
        }
    }

When I run the program the JOptionPane does not show up. I simply want it to show a message box with the true/false result on it as I have set it up already.

  • 1
    Swing GUIs should be created on the EDT. – Andrew Thompson Sep 04 '16 at 14:44
  • I tested your code, and works fine, i don't saw a problem, the `JOptionPane` displays correctly. – TimeToCode Sep 04 '16 at 15:09
  • Actually, it is working. It is just showing behind the rest of the applications. I couldn't find a quick solution to make it show on top but if you use `int tNumber = Integer.parseInt( JOptionPane.showInputDialog("Please enter your six digit ticket number: ") );` instead of the scanner, then your problem will be solved. – funaquarius24 Sep 04 '16 at 15:30
  • Also consider `boolean evaluation = tNumber % 7 == lastDigit;` – trashgod Sep 04 '16 at 15:40

2 Answers2

0

As I can remember the dialog may have relative object which will be right under the dialog but in your case it is null

JOptionPane.showMessageDialog(null, "This number is a " + evaluation + " ticket number.");

As a rule, the JOptionPane should use some container to relate to lets say the JFrame for example;

not tested

JFrame f=new JFrame();

JOptionPane.showMessageDialog(f, "This number is a " + evaluation + " ticket number.");

Please do comment if you have some additional questions

cbhogf
  • 91
  • 13
  • *"not tested"* You also failed to glance at the Java Docs. for [`JOptionPane.showMessageDialog(parentComponent,message)`](http://docs.oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html#showMessageDialog-java.awt.Component-java.lang.Object-) *"`parentComponent` - determines the `Frame` in which the dialog is displayed; if `null`, or if the `parentComponent` has no `Frame`, a **default `Frame`** is used"* – Andrew Thompson Sep 05 '16 at 12:48
  • @AndrewThompson the default frame, yes, but if you want to have JOptionPane being visible above some specific frame you should point that; BTW I say `"should use some container..."` not `"must use..."` so I don't quote docs but just point some kind of common usage case :) – cbhogf Sep 05 '16 at 14:14
  • *"..above some specific frame.."* One that, in the code example in your answer, is not even set *visible.* – Andrew Thompson Sep 05 '16 at 22:40
0

You could could use a JFrame as parent which is configured as alwaysOnTop. However then your MessageDialog also shows above all Windows.

...
JFrame f = new JFrame();
f.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(f, "This number is a " + evaluation + " ticket number.");
f.dispose();
...
Stefan
  • 444
  • 5
  • 16