Question

My code is:

    String bokstaver = Bokstaver.getText();
    String tall = Tall.getText();

    if (bokstaver.isEmpty() || bokstaver.length() < 1) {
        Bokstaver.setBackground(Color.red);
    }

    if (tall.isEmpty() || tall.length() < 5) {
        Tall.setBackground(Color.red);
    }

    if(tall.length() == 4 && bokstaver.length() == 1){
        //skriver inn string for registreringsnummer
        registreringsnummer = bokstaver + " " + tall;
        dispose();
    }

The code is written in a jButtonMouseClicked method. If I type the correct type in the two text fields, the window wont close. That is for both the first time the button is clicked, and the rest after that. If I have the dispose() outside the if - loop, it works, so what am I doing wrong?

Was it helpful?

Solution

Your if statement is not being satisfied. Make sure that:

tall.length() == 4

AND

bokstaver.length() == 1

A valid input for the Tall JTextField would be:

abcd

and

a

for the Bokstaver JTextField

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top