Domanda

I am using a custom cellrenderer for a JList that create a JPanel for each value in the model.

I want to change the mouse cursor for one component of the JPanel.

But it seems that JList doesn't dispatch mouse movement / position to the childs, and my cursor is not updated.

Here is the tree of my JList :

JList
    Custom Cell Renderer
        Custom Cell (JPanel)
            Components
            My component with mouse cursor changed

How can I make the JList dispatch mouse postion ?

Thanks.

EDIT : some code :

public class JCOTSDisplay extends JList
{
    public JCOTSDisplay()
    {
        setCellRenderer(new COTSListCellRenderer());
        setModel(.....);
    }
}

public class COTSListCellRenderer implements ListCellRenderer
{
    @Override
    public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus)
    {
        return new JCOTSCell((COTS) value);
    }
}

public class JCOTSCell extends JPanel
{
    public JCOTSCell(final COTS cots)
    {
        initComponents();
    }

    private void initComponents()
    {
        JLabel lblUrl = new JLabel("<url>");
        lblUrl.setCursort(new Cursort(Cursor.HAND_CURSOR));
    }
}
È stato utile?

Soluzione

Ok, so a JList is display only, it behave like if the components are rendered as an image, so any mouse listerner / actions will not be fired / dispatched.

I have replaced my JList with a JPanel with a GridLayout with 0 row and 1 column.

I have instantiated my model and my cell renderer and used them like the JList does.

And now everything works like I want.

Thanks.

Altri suggerimenti

If I understand correctly, you have a JList of items, some of which may be hyperlinks and you want a HAND cursor for just these items? As mentioned @kleopatra, the decoration of these items would be handled by the renderer, but the custom cursor would be handled by a listener on the JList.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("unchecked")
public class JListHoverDemo implements Runnable
{
  private JList jlist;
  private Cursor defaultCursor;

  public static void main(String args[])
  {
    SwingUtilities.invokeLater(new JListHoverDemo());
  }

  public void run()
  {
    Object[] items = new String[] {
        "One", "Two", "http://www.stackoverflow.com",
        "Four", "Five", "http://www.google.com", "Seven"
    };

    jlist = new JList(items);
    jlist.setCellRenderer(new HyperlinkRenderer());
    jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jlist.setVisibleRowCount(5);

    jlist.addMouseMotionListener(new MouseMotionAdapter()
    {
      @Override
      public void mouseMoved(MouseEvent event)
      {
        adjustCursor(event.getPoint());
      }
    });

    defaultCursor = jlist.getCursor();

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new JScrollPane(jlist));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }

  private void adjustCursor(Point point)
  {
    Cursor cursor = defaultCursor;
    int index = jlist.locationToIndex(point);

    if (index >= 0)
    {
      Object item = jlist.getModel().getElementAt(index);
      if (isHyperlink(item))
      {
        cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
      }
    }
    jlist.setCursor(cursor);
  }

  private boolean isHyperlink(Object item)
  {
    String text = item == null ? "" : item.toString();
    return text.startsWith("http");
  }

  private class HyperlinkRenderer extends DefaultListCellRenderer
  {
    @Override
    public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean hasFocus)
    {
      Component comp = super.getListCellRendererComponent(
          list, value, index, isSelected, hasFocus);

      if (isHyperlink(value))
      {
        setFont(comp.getFont().deriveFont(Font.ITALIC));
        setForeground(Color.BLUE);
      }

      return comp;
    }
  }
}

Have you tried to use method setCursor on your Custome Cell ?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top