I created a project using VLCJ and a probability function to create a media player that plays your favorite media more often than the others, because I wanted to hear my favorite music and videos more than my least favorite. This involved a lot of event listeners that were all at the bottom of my main class file where the components that register the listeners reside. This seemed like a bad way to go about it.

I decided to move each listener to it's own class in the same package and added callback methods in my main class. This worked well, but after several listeners I had to start sifting through to find non-listener classes.

I then decided to put all the listeners in their own package. Now all my listeners and callback methods have to be public.

What is the best way to keep listeners separate from their callback methods? Public callback methods sounds like bad design to me.

I register all the listeners like this,

    // UI listeners
    private final AIPKeyListener keyListener = new AIPKeyListener(this);

This instantiation is with a public AIPListener class in another package.

 *         A custom KeyListener used to listen for key events triggered from an EmbeddedMediaPlayerComponent's VideoSurfaceComponent
public final class AIPKeyListener implements KeyListener {

    // The playlist that contains the methods needed to be called when events are triggered
    AIPlaylist playlist;

    /**       Creates a custom KeyListener used to listen for key events triggered from an EmbeddedMediaPlayerComponent's VideoSurfaceComponent.
     * @param playlist as the playlist that registers this listener with an embeddedMediaPlayerComponent's videoSurfaceComponent.
    public AIPKeyListener(AIPlaylist playlist){
        this.playlist = playlist;

    public void keyTyped(KeyEvent e) {


    /* (non-Javadoc)
     * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_COMMA) {
        } else if(e.getKeyCode() == KeyEvent.VK_PERIOD) {
        } else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
        } else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
        } else if(e.getKeyCode() == KeyEvent.VK_L) {
        else if(e.getKeyCode() == KeyEvent.VK_R) {
        } else if(e.getKeyCode() == KeyEvent.VK_P) {
            playlist.getLogger().finest("Resetting probabilities");

    public void keyPressed(KeyEvent e) {



The callback methods are in the same class where the listeners are registered.

/** Switches whether looping is enabled or not.
    public void loopSwitch() {
        if(looping) {
            aIPlaylistLogger.finest("Turning loop off");
            if(repeating) {
            looping = false;
        } else {
            aIPlaylistLogger.finest("Turning loop on");
            looping = true;

The full code is here: https://github.com/aljohnston112/AIPlaylist/releases/tag/v0.1.0-0.1.0

My main concern is that having public callback methods callable by anyone is bad practice, but I want to be able to keep the listeners separate from the rest of my project. What is the best way to go about this?



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.