4

There are a lot of discussions on web on how to have hyperlinks in swing and JTable, e.g. HyperLink in JTable Cell.

The approach above is problematic because it only knows which cell the mouse is in, not the exactly text it is on, which means:

  1. Can not handle multiple hyperlinks in the same cell;
  2. Can not make the mouse cursor showing intuitively. Whenever the mouse is in the cell that has hyperlink, the mouse will become hand shape, even when the mouse points to some normal text or even emtpy area.

Another approach is to display JEditorPane in the cell but is also problematic because JTable only uses the JComponent returned by cell renderer to draw, I don't think the object will be sent any events. Because the default renderer will re-use the component for every cell, so it doesn't make any sense to have it handle any events.

So I wonder what's the best way to achieve the above effect.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Kan Li
  • 8,557
  • 8
  • 53
  • 93
  • See also [How to make a JButton in a JTable cell click-able?](http://stackoverflow.com/questions/5555938/how-to-make-a-jbutton-in-a-jtable-cell-click-able) – Jonas Aug 12 '11 at 16:57
  • Thanks Jonas, I haven't got a time to look at it carefully, but the first glance seems I need a cell editor. Seems like it can only handle clicks, right? What if I want to handle the events like mouseMove, so the cursor shape can be changed correctly? Thanks. – Kan Li Aug 12 '11 at 17:06

1 Answers1

3

Another approach is to display JEditorPane in the cell but is also problematic because JTable only uses the JComponent returned by cell renderer to draw, I don't think the object will be sent any events.

Try placing the cell in edit mode every time the cell gains focus. Then the editor should be display which is a real component and it should reaceive all the events. Something like:

JTable table = new JTable(...)
{
    //  Place cell in edit mode when it 'gains focus'

    public void changeSelection(int row, int column, boolean toggle, boolean extend)
    {
        super.changeSelection(row, column, toggle, extend);

        if (editCellAt(row, column))
        {
            Component editor = getEditorComponent();
            editor.requestFocusInWindow();
        }
    }
};

I would customize the code to only edit cell for the specific column.

camickr
  • 321,443
  • 19
  • 166
  • 288