itro itro - 2 months ago 20
Java Question

how to add document Listener to the JTextFields inside a panel?

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


}

Answer

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