answersLogoWhite

0


Best Answer

An Adapter is simply a concrete class which implements all the methods of a Listener interface as empty functions. They are convenience classes made because of the tendency to implement Listeners as anonymous classes.

For example, let's say we want to add a Listener to a JFrame to detect a mouse click:

// implementing a Listener

JFrame frame = new JFrame();

frame.addMouseListener(new MouseListener() {

void mouseClicked(MouseEvent e) {

// do something here

}

void mouseEntered(MouseEvent e) {

}

void mouseExited(MouseEvent e) {

}

void mousePressed(MouseEvent e) {

}

void mouseReleased(MouseEvent e) {

}

});

Note how this has a lot of extra code that does nothing.

// implementing an Adapter

JFrame frame = new JFrame();

frame.addMouseListener(new MouseAdapter() {

void mouseClicked(MouseEvent e) {

// do something here

}

});

Note now how we only need to implement one method. This is much cleaner and easier to read.

User Avatar

Wiki User

16y ago

Still curious? Ask our experts.

Chat with our AI personalities

MaxineMaxine
I respect you enough to keep it real.
Chat with Maxine
ProfessorProfessor
I will give you the most educated answer.
Chat with Professor
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve

Add your answer:

Earn +20 pts
Q: What is the function of adapter in java model?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions