Search code examples
javaswingdispose

Problems with dispose() commando in Java Swing


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?


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