Stack Exchange
Java exceptions pop up window
My question is how do I make a pop up message when a user puts in the wrong input, it has to do with filling in the right string (rood, groen, oranje) with the right colour, but when he doesnt put anything in the textfield or the wrong name (string) it should return an error with a popup window.
Snippet below:
Java class snippet:
    int Kleurteller;
    for (Kleurteller = 1; Kleurteller <= 7; Kleurteller++) {
        if (kleur != null) {
            if (kleur.equals("rood")) {
                g.setColor(Color.red);
                g.fillOval(795, 45, 30, 30);
            } else if (kleur.equals("groen")) {
                g.setColor(Color.green);
                g.fillOval(795, 45, 30, 30);
            } else if (kleur.equals("oranje")) {
                g.setColor(Color.orange);
                g.fillOval(795, 45, 30, 30);
            }
            else {
            try{}
            catch (Exception e){
            JOptionPane.showMessageDialog(null, "geen juiste kleur opgegeven, kies rood, groen of oranje");

            }

            }

        }
    }

In my mainclass i have a path to the file so it will run it. (SetContentPane).
Currently does not show the redlines anymore when a user put the wrong things in the textfield. Dont get a pop up screen, which i need for my program

just let the try/catch away:
 int Kleurteller;
    for (Kleurteller = 1; Kleurteller <= 7; Kleurteller++) {
        if (kleur != null) {
            if (kleur.equals("rood")) {
                g.setColor(Color.red);
                g.fillOval(795, 45, 30, 30);
            } else if (kleur.equals("groen")) {
                g.setColor(Color.green);
                g.fillOval(795, 45, 30, 30);
            } else if (kleur.equals("oranje")) {
                g.setColor(Color.orange);
                g.fillOval(795, 45, 30, 30);
            }
            else {
                JOptionPane.showMessageDialog(null, "geen juiste kleur opgegeven, kies rood, groen of oranje");
            }
        }
    }

Btw, I recommend you to always begin variable names with a lowercase letter, makes it easier to differentiate between class names and variable names.
And if you do it like that, you don't even have to check that kleur is not null:
        if ("rood".equals(kleur)) {
            g.setColor(Color.red);
            g.fillOval(795, 45, 30, 30);
        } else if ("groen".equals(kleur)) {
            g.setColor(Color.green);
            g.fillOval(795, 45, 30, 30);
        } else if ("oranje".equals(kleur)) {
            g.setColor(Color.orange);
            g.fillOval(795, 45, 30, 30);
        }
        else {
            JOptionPane.showMessageDialog(null, "geen juiste kleur opgegeven, kies rood, groen of oranje");
        }


ok that works, but when u did run the program, this message stays up and u cant close it. Only way possible is to close the program in order to close the tab. Idea to fix this?

there should be an 'ok' button on the dialog, no?

yes there is, everything seems lay-outed fine, but when u want to click on it it doesnt close.

I can press on the okay button, but it instantly goes away and then comes back and keeps spamming like a bug, I can’t close it it just keeps coming back it feels like an infinite window close and reopen

ah, now I got it: it's because you have the dialog inside the for-loop! it appears 6 times, right? you don't need the for-clause. just use the second code I posted.

even without the for loop, still having this issue.

please post the full code, I can't help you otherwise. if the dialog appears multiple times, you must have a loop somewhere
