1

I have a class which extends JPanel, inside this panel i have added a lot of textField. I want to add DocumentListener to every textField to detect changes to the text and change the bachground of txtField to red. I did something as below but not yet working.

Can anyone help to solve it or any new suggestion is welcome?

public class MainDetailPanel extends JPanel implements DocumentListener{
public JLabel lblS_DWLDIRPRDPIC,lblL_DWLDIRPRD,lblS_UPLDIR,lblL_UPLDIR,lblS_DWLDIRPRD;
public JLabel lblFTP_DEFDIR,lblFTP_SERVER,lblFTP_POORT,lblFTP_USER,lblFTP_PASWOORD,lblL_CFDIR;
public JLabel lblL_DWLDIRPRDPIC,lblS_CFDIR,lblS_CFNAME,lblS_DWLNAMEPRD,lblS_DWLNAMEPRDPIC;

public JTextField txtL_DWLDIRPRD,txtS_DWLDIRPRDPIC,txtL_UPLDIR,txtS_DWLNAMEPRDPIC,txtL_CFDIR;
public JTextField txtFTP_DEFDIR,txtFTP_SERVER,txtFTP_POORT,txtFTP_USER,txtFTP_PASWOORD,txtS_UPLDIR;
public JTextField txtL_DWLDIRPRDPIC,txtS_CFDIR,txtS_CFNAME,txtS_DWLNAMEPRD,txtS_DWLDIRPRD;
public MainDetailPanel() {
    createComponents();
    layoutComponents();
}

private void createComponents() {
    txtL_DWLDIRPRD      = new JTextField(30);
    txtL_DWLDIRPRD.getDocument().addDocumentListener(this);
    txtS_DWLDIRPRDPIC   = new JTextField(30);
    txtL_UPLDIR         = new JTextField(30);
    txtS_DWLNAMEPRDPIC  = new JTextField(30);
    txtFTP_DEFDIR       = new JTextField(30);
    txtFTP_SERVER       = new JTextField(30);
    txtFTP_POORT        = new JTextField(30);
    txtFTP_USER         = new JTextField(30);
    txtFTP_PASWOORD     = new JTextField(30);
    txtL_CFDIR          = new JTextField(30);
    txtL_DWLDIRPRDPIC   = new JTextField(30);
    txtS_UPLDIR         = new JTextField(30);
    txtS_CFDIR          = new JTextField(30);
    txtS_CFNAME         = new JTextField(30);
    txtS_DWLNAMEPRD     = new JTextField(30);
    txtS_DWLDIRPRD      = new JTextField(30);

    lblFTP_DEFDIR       = new JLabel("FTP_DEFDIR");
    lblFTP_SERVER       = new JLabel("FTP_SERVER");
    lblFTP_POORT        = new JLabel("FTP_POORT");
    lblFTP_USER         = new JLabel("FTP_USER");
    lblFTP_PASWOORD     = new JLabel("FTP_PASWOORD");
    lblL_CFDIR          = new JLabel("L_CFDIR");
    lblL_UPLDIR         = new JLabel("L_UPLDIR");
    lblL_DWLDIRPRDPIC   = new JLabel("L_DWLDIRPRDPIC");
    lblS_CFDIR          = new JLabel("S_CFDIR");
    lblS_CFNAME         = new JLabel("S_CFNAME");
    lblS_DWLNAMEPRD     = new JLabel("S_DWLNAMEPRD");
    lblS_DWLDIRPRD      = new JLabel("S_DWLDIRPRD");
    lblS_DWLNAMEPRDPIC  = new JLabel("S_DWLNAMEPRDPIC");
    lblS_UPLDIR         = new JLabel("S_UPLDIR");
    lblS_DWLDIRPRDPIC   = new JLabel("lblS_DWLDIRPRDPIC");
    lblL_DWLDIRPRD      = new JLabel("lblL_DWLDIRPRD");
}

private void layoutComponents() {
    setLayout(new ParagraphLayout());
    add(lblFTP_DEFDIR, ParagraphLayout.NEW_PARAGRAPH);
    add(txtFTP_DEFDIR);
    add(lblFTP_SERVER, ParagraphLayout.NEW_PARAGRAPH);
    add(txtFTP_SERVER);
    add(lblFTP_POORT, ParagraphLayout.NEW_PARAGRAPH);
    add(txtFTP_POORT);
    add(lblFTP_USER, ParagraphLayout.NEW_PARAGRAPH);
    add(txtFTP_USER);
    add(lblFTP_PASWOORD, ParagraphLayout.NEW_PARAGRAPH);
    add(txtFTP_PASWOORD);
    add(lblL_CFDIR, ParagraphLayout.NEW_PARAGRAPH);
    add(txtL_CFDIR);
    add(lblL_UPLDIR, ParagraphLayout.NEW_PARAGRAPH);
    add(txtL_UPLDIR);
    add(lblL_DWLDIRPRD, ParagraphLayout.NEW_PARAGRAPH);
    add(txtL_DWLDIRPRD);
    add(lblL_DWLDIRPRDPIC, ParagraphLayout.NEW_PARAGRAPH);
    add(txtL_DWLDIRPRDPIC);
    add(lblS_UPLDIR, ParagraphLayout.NEW_PARAGRAPH);
    add(txtS_UPLDIR);
    add(lblS_CFDIR, ParagraphLayout.NEW_PARAGRAPH);
    add(txtS_CFDIR);
    add(lblS_CFNAME, ParagraphLayout.NEW_PARAGRAPH);
    add(txtS_CFNAME);
    add(lblS_DWLNAMEPRD, ParagraphLayout.NEW_PARAGRAPH);
    add(txtS_DWLNAMEPRD);
    add(lblS_DWLDIRPRD, ParagraphLayout.NEW_PARAGRAPH);
    add(txtS_DWLDIRPRD);
    add(lblS_DWLNAMEPRDPIC, ParagraphLayout.NEW_PARAGRAPH);
    add(txtS_DWLNAMEPRDPIC);
    add(lblS_DWLDIRPRDPIC, ParagraphLayout.NEW_PARAGRAPH);
    add(txtS_DWLDIRPRDPIC);
}

public void insertUpdate(DocumentEvent e) {
    ((JTextField)e.getDocument()).setBackground(Color.red);
}

public void removeUpdate(DocumentEvent e) {
    ((JTextField)e.getDocument()).setBackground(Color.red);
}

public void changedUpdate(DocumentEvent e) {
    ((JTextField)e.getDocument()).setBackground(Color.red);
}


}
itro
  • 7,006
  • 27
  • 78
  • 121
  • What is not working? What do you expect, and what happens instead? – JB Nizet May 23 '12 at 10:03
  • Exception in thread "AWT-EventQueue-1" java.lang.ClassCastException: javax.swing.text.PlainDocument at de.util.scanners.view.MainDetailPanel.insertUpdate(MainDe‌​tailPanel.java:107) – itro Jul 04 '12 at 10:19

1 Answers1

10

You can't cast the Document to a JTextField. JTextField has a document. But JTextField is not a Document.

AFAIK, you'll have to use a separate listener for every JTextField, and give the reference of the JTextField to your listener:

private static class BecomingRedDocumentListener implements DocumentListener {
    private JTextField textField;

    public BecomingRedDocumentListener(JTextField textField) {
        this.textField = textField;
    }
    @Override
    public void insertUpdate(DocumentEvent e) {
        textField.setBackground(Color.red);
    }
    @Override
    public void removeUpdate(DocumentEvent e) {
        textField.setBackground(Color.red);
    }
    @Override
    public void changedUpdate(DocumentEvent e) {
        textField.setBackground(Color.red);
    }
}

And you should use a factory method to create your textfields:

private JTextField createTextFieldBecomingRed() {
    JTextField tf = new JTextField(30);
    tf.addDocumentListener(new BecomingRedDocumentListener(tf));
    return tf;
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks it is working but in my case there is a small problem.At the begin of creating this panel everything is OK(bg is white).Later By clicking on a file, these text Fields gets default value, at this time also the bg must stay white. ***How Can i solve this issue?*** – itro May 23 '12 at 11:50
  • Second problem is that when something new added, bg should be red and if we delete new added character, bg should be white. Also when a charakter deleted from original text, bg should be red and if we set a character back, bg should be white. – itro May 23 '12 at 12:02
  • 1
    Sincerely, you should be able to figure this out by yourself. First problem: deactivate the listeners somaway, or reset the color to white after the field is populated. Second problem: compere the neww text with the original one, and set the background color accordingly. – JB Nizet May 23 '12 at 12:49
  • I'm pretty sure `tf.addDocumentListener()` won't work for the reason you gave: that a JTextField is not a Document. Shouldn't it be `tf.getDocument().addDocumentListener()` instead? – Jeff Holt Jul 30 '21 at 20:16