Question

I am trying to set gmailPasswordField.setVisible(true) & senderPasswordLabel.setVisible(true) if the JTextField is changed, but it's not working.

doc.addDocumentListener(
  new DocumentListener() {
    public void changedUpdate(DocumentEvent e) {
      int minLengthEmail = "n@gmail.com".length();
      if (countDocumentChangeLength > minLengthEmail) {
        gmailPasswordField.setVisible(true); // Doesn't appear to do anything
        senderPasswordLabel.setVisible(true); // Nor this
      }
    }
  });

No correct solution

OTHER TIPS

Did you add a System.out.println(...) to see if the code is being executed? This is a basic debugging technique to see if the code is executing as you expect.

The changedUpdate(...) method is invoked when you change an attribute of the text.

I suspect you want to implement the other DocumentListener methods so you will be notified when the "text" in the Document changes. Read the section from the Swing tutorial on How to Write a DocumentListener for more information and working examples.

Don't use the method setVisible(boolean). Use CardLayout instead.

Little howto:

  1. Move your widgets, which visibility must be switched, into a new JPanel (layout panel).
  2. Create another JPanel with CardLayout (card panel) and place the layout panel into it.
  3. Place an empty JPanel into the card panel.
  4. Make sure that the appropriate card is visible (in your case - the empty card).
  5. Make another card visible when required (in your document listener).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top