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);
}
});