0

In my code when I try to add a FocusListener to a JTextField, it says

ChatClient.java:30: error: incompatible types: ChatClient cannot be converted to FocusListener
    text.addFocusListener(this);

But adding a MouseListener works just fine. Why is it like that? Creating another class with FocusListener works. I would like to know the difference between adding a MouseListener and a FocusListener. Is there any other way to simply add FocusListener without writing a separate class for it?

public void makeUI(){
    text = new JTextField(defaultMessage);
    text.setBounds(10,620,295,40);
    text.addFocusListener(this);
    add(text);
    button = new JButton("SEND");
    button.setBounds(310,620,80,40);
    button.setForeground(Color.WHITE);
    button.setBackground(Color.decode("#11A458"));
    button.setFocusPainted(false);
    button.addActionListener(this);
    add(button);
    setSize(400,700);
    setLayout(null);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  public void focusGained(FocusEvent ae){
    if(text.getText().equals(defaultMessage)){
        text.setText("");
    }
  }
  public void focusLost(FocusEvent ae){
    if(text.getText().isEmpty()){
      text.setText(defaultMessage);
    }
  }
Jasperan
  • 2,154
  • 1
  • 16
  • 40
draner
  • 43
  • 13
  • 2
    your ChatClient simply is not a FocusListener, but it probably is an ActionListener. How is `ChatClient` declared, it probably only implements one interface. – luk2302 Aug 19 '17 at 14:07
  • 1
    That said: writing another class for it (and for your other listeners) would be cleaner. Have you learnt about inner and anonymous inner classes yet? – JB Nizet Aug 19 '17 at 14:09
  • 2
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 19 '17 at 14:14
  • I'm with @JBNizet. By making your GUI (your "view") implement listener interfaces, you're also making it both view and "controller", giving the too much responsibility. While this may be OK for very small toy or demonstration programs, it can make things unwieldy if/when the program gets larger. Much better to separate the responsibilities into their own separate classes, an inner class if trivial or even a separate stand-alone class if not. – DontKnowMuchBut Getting Better Aug 19 '17 at 15:33

1 Answers1

2

I see that you have added focusGained and focusLost methods, but does your class implement FocusListener interface?

So, the code should be something like:

/**
 * 1. this implements FocusListener is important
 * 2. another interfaces you had here before also should be present
 */
public class YourClass implements FocusListener {  

        public void makeUI(){
            text = new JTextField(defaultMessage);
            text.setBounds(10,620,295,40);
            text.addFocusListener(this);
            add(text);
            button = new JButton("SEND");
            button.setBounds(310,620,80,40);
            button.setForeground(Color.WHITE);
            button.setBackground(Color.decode("#11A458"));
            button.setFocusPainted(false);
            button.addActionListener(this);
            add(button);
            setSize(400,700);
            setLayout(null);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

        @Override
        public void focusGained(FocusEvent ae){
            if(text.getText().equals(defaultMessage)){
                text.setText("");
            }
        }

        @Override
        public void focusLost(FocusEvent ae){
            if(text.getText().isEmpty()){
                text.setText(defaultMessage);
            }
        }

}
iVetal
  • 125
  • 5