我有一个扩展JPanel的类,在这个面板中我添加了很多textField。 我想将DocumentListener添加到每个textField以检测文本的更改并将txtField的bachground更改为红色。 我做了如下的事情,但还没有工作。
任何人都可以帮忙解决,或欢迎任何新建议吗?
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);
}
}
答案 0 :(得分:9)
您无法将Document强制转换为JTextField。 JTextField 具有文档。但是JTextField is not a
文档。
AFAIK,你必须为每个JTextField使用一个单独的监听器,并将JTextField的引用提供给你的监听器:
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);
}
}
您应该使用工厂方法来创建文本字段:
private JTextField createTextFieldBecomingRed() {
JTextField tf = new JTextField(30);
tf.addDocumentListener(new BecomingRedDocumentListener(tf));
return tf;
}