0

I'm having trouble with making JButton enabled when two text fields in the same panel are equal. I tried adding an ActionListener but still nothing is happening and my JButton stays disabled. The same thing happened when I tried simply str1.contentEquals(str2).

txtPassword = new JTextField();
txtPassword.setForeground(Color.LIGHT_GRAY);
txtPassword.setText("password");
txtPassword.setBounds(115, 49, 200, 20);
panel.add(txtPassword);
txtPassword.setColumns(10);
String str1 = txtPassword.getText();

txtRepeatPassword = new JTextField();
txtRepeatPassword.setForeground(Color.LIGHT_GRAY);
txtRepeatPassword.setText("repeat password");
txtRepeatPassword.setBounds(115, 124, 200, 20);
panel.add(txtRepeatPassword);
txtRepeatPassword.setColumns(10);
String str2 = txtRepeatPassword.getText();

JButton btnNewButton = new JButton("Next >>>");
btnNewButton.setVisible(true);
btnNewButton.setEnabled(false);

btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

        panelPYT.setVisible(true);
        panel.setVisible(false);

    }
});
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 15));
btnNewButton.setBounds(88, 182, 259, 50);
panel.add(btnNewButton);

txtPassword.getDocument().addDocumentListener(new DocumentListener() {
      public void changedUpdate(DocumentEvent e) {
            same();
          }
          public void removeUpdate(DocumentEvent e) {
            same();
          }
          public void insertUpdate(DocumentEvent e) {
            same();
          }
          public void same() {
                 if (str1.equals(str2)){
                   btnNewButton.setEnabled(true);
                 }
                 else {
                   btnNewButton.setEnabled(false);
                }
          }
});

JButton btnNext = new JButton("Next >>>");
btnNext.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        panelPYT.setVisible(false);
        panel.setVisible(true);
    }
});
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 1
    1) Welcome to Stack Overflow, please take the [tour] and read [ask], there you will find a link to how to create a [mre] which is a good starting point for us to help you. 2) Don't use `null-layout` and `setBounds` as [null layout is evil](https://www.leepoint.net/GUI/layouts/nulllayout.html) and causes a lot of problems like [this one](https://stackoverflow.com/a/42521097/2180785) when tested on other computers with different OS / screen sizes / resolutions / PLAFs / etc. – Frakcool Nov 13 '19 at 19:53
  • 1
    3) You have a method `same()` inside your `DocumentListener` that doesn't seem right. 4) You only added your `DocumentListener` to the `txtPassword` field but not to `txtRepeatPassword`, probably you want to add it to it as well. – Frakcool Nov 13 '19 at 19:56
  • 1
    `if (str1.equals(str2))` will always evaluate to the same result, because you only assign str1 and str2 once. The `=` statement is a *one time assignment.* It does not create an eternal link between that variable and any future values that the getText() method might return. – VGR Nov 13 '19 at 20:08

2 Answers2

1

You're adding your listener to txtPassword, but I'm assuming that the user is entering their password into txtPassword first, and txtRepeatPassword second. Which means that when the user is done typing in the first field, the DocumentListener is done firing, and when they enter their password into the second one, the listener won't fire, because the typing is happening in txtRepeatPassword.

Long story short, just add the DocumentListener to both text fields.

DocumentListener dlist = new DocumentListener(){
    .... all your methods
};

txtPassword.getDocument().addDocumentListener(dlist);
txtRepeatPassword.getDocument().addDocumentListener(dlist);

Also you need to change

if (str1.equals(str2)){
    btnNewButton.setEnabled(true);
}else {
    btnNewButton.setEnabled(false);
}

to

if (txtPassword.getText().equals(txtRepeatPassword.getText())){
    btnNewButton.setEnabled(true);
}else {
    btnNewButton.setEnabled(false);
}

The way you had it, str1 and str2 were just holding the value of the their respective text boxes at the time of initialization. You want to compare the values they hold at the time of the listener firing, so you'll have to retrieve the value at that time.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mypetlion
  • 2,415
  • 5
  • 18
  • 22
  • Thanks, but that still doesn't solve the problem, I have repeated the same method but for str2 and the result is still the same – Antoni Skup Nov 13 '19 at 20:03
  • I think that the problem has to be with the method, because when i change the if statement " (str1.equals(str2))" to " (str.equals("bruh")) and i insert bruh it still doesn't work – Antoni Skup Nov 13 '19 at 20:09
  • @AntoniSkup Oh I see what you did. I'll edit my answer in a minute here. – mypetlion Nov 13 '19 at 20:46
  • @AntoniSkup I've added to my answer. Let me know if that solves it for you. – mypetlion Nov 13 '19 at 21:16
  • Thanks man :D I put the String str1 = textPassword.getText(); String str2 = textRepeatPassword.getText(); if (str1.equals(str2)){ in the button action performed section so each time i press it it checks if those two fields are equal – Antoni Skup Nov 13 '19 at 21:38
0

if (str1.equals(str2)){

The values of the strings don't change just because you type into a text field.

You need to get the text from the text field each time you do the check:

String str1 = textPassword.getText();
String str2 = textRepeatPassword.getText();

if (str1.equals(str2)){
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Ahhh thanks man, this made me realise I need to put that in the button.actionPerformed section – Antoni Skup Nov 13 '19 at 21:36
  • @AntoniSkup *I need to put that in the button.actionPerformed section* - no, that doesn't make sense. You need to invoke that logic from the DocumentListener code. The point of the code is to ENABLE the button only when the passwords match. So if the button is enabled you know the passwords match there is no need to do a check. – camickr Nov 13 '19 at 22:05