Brandon
Brandon

Reputation: 4523

Forward mouse events from JList to the items in the list

Currently, I have a Jlist with a custom CellRenderer as follows. I attached a custom MouseListener in order to capture hover events and, later on, double click events; but the events weren't being fired when I added them this way. After 20 minutes, I found out it was because the JList was blocking the events from reaching the individual list items. Is there any way I can force the JList to forward the mouse events to its children elements, or is there a better way of doing this.

My CellRenderer:

public class FriendRenderer extends DefaultListCellRenderer {

    private static final long serialVersionUID = 1L;

    public Component getListCellRendererComponent(JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean hasFocus) {

        Friend friend = (Friend)value;

        JLabel label = new JLabel(friend.getName());

        JPanel mainOuterPanel = new JPanel();
        mainOuterPanel.setLayout(new BorderLayout());

        /* Adding components to mainOuterPanel... */

        /* Here is where I add the custom MouseListener */
        mainOuterPanel.addMouseListener(new ListItemMouseListener());

        return(mainOuterPanel);
    }

}

My MouseListener:

public class ListItemMouseListener implements MouseListener{

    @Override
    public void mouseClicked(MouseEvent e) {}

    @Override
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseReleased(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {
        Component source = e.getComponent();
        source.setBackground(new Color(102, 99, 86));
        System.out.println("Mouse entered on " + source.toString());
    }

    @Override
    public void mouseExited(MouseEvent e) {
        Component source = e.getComponent();
        source.setBackground(null);
        System.out.println("Mouse Exited on " + source.toString());
    }
}

Where I build my JList in my JFrame:

private JList getFriendsList(){
    friendsList = new JList(getFriendsListModel()); //TODO Actually fill with data
    friendsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    friendsList.setLayoutOrientation( JList.VERTICAL );
    friendsList.addListSelectionListener(new FriendsListSelectionListener());
    friendsList.setCellRenderer(new FriendRenderer());
    return friendsList;
}

Thanks in advance!

Upvotes: 1

Views: 3134

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

Add the listener to the list and use the list methods to convert the point to a row.

The reason it is not working when added to the renderer, is that the render is really only used for ..rendering. It is like a 'template' used to paint the list item (& every other visible list item) before the item(s) appear on-screen.


..access the cells .. in order to change the background..

Configure the ListCellRenderer to handle that.

Upvotes: 3

Related Questions