CodeCompletionForAnonymousClasses

Last edit January 16, 2003
All of the below applies for EclipseIde and IntellijIdea, although the key bindings vary somewhat
Eclipse has code completion for anonymous classes:

    frame.addMouseListener(new M<caret is here>);

typing CTRL+SPACE and selecting 'MouseAdapter' gives you

    frame.addMouseListener(new MouseAdapter);

then you type in '(){}' and add enter as you see fit to get something like:

    frame.addMouseListener(new MouseAdapter()
    {
    <caret is here> 
    }
    );

typing CTRL+SPACE will show a list of methods you can override and selecting mouseClicked' gives you

    frame.addMouseListener(new MouseAdapter()
    {
      public void mouseClicked(MouseEvent e) {
      }
    }
    );

as such, you can easily override other methods, now or later, as you like.

and IDEA:

  • IDEA also offers definable 'templates': Type e.g. 'itco' and hit TAB and it'll generate the code for an iteration of the elements of a collection. Templates can have variable parts you can step through with the TAB key and modify them.
  • IDEA has code completion for anonymous classes (useful for event handlers):

    frame.addMouseListener(new <caret is here>);

typing CTRL+SHIFT+SPACE and selecting 'MouseAdapter' opens a list of overridable methods and gives the following code (depending on the selection of overridable methods):

    frame.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
      }

public void mousePressed(MouseEvent e) { }

public void mouseReleased(MouseEvent e) { }

public void mouseEntered(MouseEvent e) { }

public void mouseExited(MouseEvent e) { } });

(similar when MouseListener is selected instead of MouseAdapter). This works on any method that takes an interface reference as a parameter.