Results 1 to 5 of 5
  1. #1
    kpwane is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default jtextfield, actionlistener and changing background of parent jpanel

    I have a series of JLabels and JTextFields for users to enter data. Each JLabel and JTextfield 'pair' (i.e. where the label is the caption for the textfield) are contained within a separate JPanel.

    What i want is for the JPanels background colour to be changed when the JTextField contained within it has got focus. i have the following code which works.

    Java Code:
            txtLastName_ANS.addFocusListener(new FocusListener() {
                public void focusGained(FocusEvent e) {
                    this.getParent().setBackground(Color.yellow);
                }
    
                public void focusLost(FocusEvent e) {
                    this.getParent().setBackground(Color.lightGray);
                }
            });
    I would like to separate this block of code out so that it can be called by any JTextField that i add an ActionListener to so that the code is not repeated multiple times.

    Is this possible? can anyone help me?

    thank you

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Don't use an anonymous inner class. Instead have a concrete class declared in a seperate file, outside of the class, or inside the class.

    If you declare it outside of the class or file you will need to pass the panel through the constructor so an inner class would be easiest.

    Java Code:
    class YourClass{
      private InnerClass implements FocusListener{
        your code for focus gained and lost
      }
    }

  3. #3
    kpwane is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    thanks, works well

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome, glad to have helped. Please mark your thread solved with the thread tools at the top.

    While anonymous inner classes are nice I try to avoid using them since they aren't so easily re-used. The best way to use an anonymous inner class is like this
    Java Code:
    FocusListener fl = new FocusListener(){
      focus listener methods
    }
    This essentially captures a focus listener and lets you re-use it whenever. Creating an inner class may offer more flexibility though since you can give it instance variables and a constructor(I think anonymous inner classes can have them too but Im not sure how exactly).

  5. #5
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    @sunde
    You can add whatever methods, constructors, and instance variables you want but you will not be able to use any of them from outside because the actual class that you are subclassing does not have concrete access them.

Similar Threads

  1. Changing background color
    By nikkka in forum New To Java
    Replies: 4
    Last Post: 03-12-2011, 06:54 AM
  2. Replies: 1
    Last Post: 11-10-2010, 11:14 PM
  3. Replies: 1
    Last Post: 01-26-2010, 12:22 AM
  4. How show a popWindow that have JPanel like parent
    By Wolverine in forum AWT / Swing
    Replies: 4
    Last Post: 05-23-2009, 09:59 AM
  5. Replies: 0
    Last Post: 08-03-2007, 05:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •