i'm trying to modify a java code. I have a jSpinner where the user have to choose a number like minimum 0 and after this step the user can continue by clicking the OK button and continue his steps...

So here what i want is when the user didn't enter any value in the jSpinner and if he clicks the OK button to continue, it must display "Error you must enter minimum value 1!" and stop to go to the next step. The user must stay in the same window.

For this what i tried is to create a piece of code like this:

public int getAlerte() {

     int x = 0;
        if (Integer.parseInt(jSpinnerParameter.getValue().toString()) <= -1 ||
            Integer.parseInt(jSpinnerParameter.getValue().toString()) >200) {
            JOptionPane.showMessageDialog(this, " ID number must be minimum 1",
                    "Alert", JOptionPane.ERROR_MESSAGE);
           x = 1; 
        }
        return x;  
}

I put this line in the starting of the code: import javax.swing.JOptionPane;

After this, when i enter -1 or nothing (initial value=0) it never display the error message.

Why the message is not displayed, could someone help please? i can't see the bug.

Thank you

Re: How to display an error message in java? 80 80

Where are you calling the getAlerte method?

Re: How to display an error message in java? 80 80

jLabelWithError.setVisible(true);
and to hide it again:
jLabelWithError.setVisible(false);

Re: How to display an error message in java? 80 80

Right.As stultuske said:take one Jlabel and use setvisible(true / false) method for displaying your error msg that you want to print.

Re: How to display an error message in java? 80 80

Genevish,
i call the getAlert methot at the end of the code.
Thank you

Re: How to display an error message in java? 80 80

OK but where can i put the new jLabel?
I can't put it in same window of the jSpinner right.
Thx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.