Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7974907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:30:41+00:00 2026-06-04T08:30:41+00:00

I have a class which extends JPanel, inside this panel i have added a

  • 0

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


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:30:42+00:00Added an answer on June 4, 2026 at 8:30 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi Experts: I have a class which extends JPanel now ShapePanel (Sp) this gets
I have this class which creates a grid: class GridPane extends JPanel{ public GridPane(int
I have a class (Timeline) which extends JPanel. The Timeline Panel contains many JLabels
I have class A which extends the Activity class. This class is in package
I have a custom class loader which extends from a URLClassLoader. I added a
I have a class PanelFormes that extends JPanel, which I use as a container.
I have created a class called SimpleCanvas which extends the JPanel class, and I
I have a class which simply extends JPanel and overrides the paintComponent method as
Class SampleFiveA extends JPanel. This contains textfields, one below the other, each of which
I have a class which extends a JPanel. Code below: public class Test extends

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.