Reputation:
Is it a good practice to add few listeners for JComponent in different part of code? Should I create one bigger listener?
For example I have JTextField, I noticed that both KeyListeners are called.
JTextField textField = new JTextField();
textField.addKeyListener(new KeyListener()
{
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
something();
}
});
textField.addKeyListener(new KeyListener()
{
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
somethingElse();
}
});
Upvotes: 0
Views: 119
Reputation: 347334
Well, it's bad practice to use KeyListener
(generally, but especially) with text components.
Most listener interfaces tend to have "adapter" class, which are just concrete implementations of the listener interface without any functionality, so you can pick and choose the methods you actually want to use
Upvotes: 3