Use of getDocument() on Jtextfield

pedro66
pedro66 used Ask the Experts™
on
need to know how to call the textfield with getdocument and a sample implementation code.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
> call the textfield with getdocument

what are you trying to do ?
Top Expert 2016
Commented:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class SmartText {

 public static void main(String[] args){
   JFrame f = new JFrame();
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Container c = f.getContentPane();
   c.add(new JColourTextField(),BorderLayout.NORTH);
   c.add(new JColourTextField(),BorderLayout.SOUTH);
   f.setSize(300,200);
   f.setVisible(true);
 }

}

class JColourTextField extends JTextField implements DocumentListener {
  public JColourTextField(){
    setBackground(Color.blue);
    getDocument().addDocumentListener(this);
  }


  public void changedUpdate(DocumentEvent e){
    //Gives notification that an attribute or set of attributes changed.
    doColor();
  }

  public void insertUpdate(DocumentEvent e){
    //Gives notification that there was an insert into the document.
    doColor();
  }

  public void removeUpdate(DocumentEvent e){
    //Gives notification that a portion of the document has been removed.
    doColor();
  }

  void doColor(){
    if(getText().trim().length() > 0){
      setBackground(Color.white);
    }
    else {
      setBackground(Color.blue);
    }
  }

}
pedro66:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for CEHJ

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial