1

我正在为 JList 使用自定义 cellrenderer,它为模型中的每个值创建一个 JPanel。

我想为 JPanel 的一个组件更改鼠标光标。

但似乎 JList 没有向孩子发送鼠标移动/位置,而且我的光标没有更新。

这是我的 JList 的树:

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

如何使 JList 调度鼠标位置?

谢谢。

编辑:一些代码:

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));
    }
}
4

3 回答 3

0

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.

于 2013-10-15T13:29:22.830 回答
0

如果我理解正确,您有一个 JList 项目,其中一些可能是超链接,您想要一个 HAND 光标仅用于这些项目?正如@kleopatra 提到的,这些项目的装饰将由渲染器处理,但自定义光标将由 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;
    }
  }
}
于 2013-10-15T15:45:42.737 回答
-1

您是否尝试在您的 Custome Cell 上使用方法setCursor ?

于 2013-10-15T10:13:46.510 回答