I would like to render text inside JTable column cells as Hyperlinks
(so the hand-cursor appears when the mouse hovers over the cells, and a
single click event on the link can be trapped.
I tried using JEditorPane as the cellrenderer for the JTable column
with a HyperLinkListener, but it doesn't seem to work - although the
text appears as a hyperlink, mouse hover over the cell does not change
the cursor. Only after clicking on the cell does the hyperlink behavior
get exposed (The JTable seems to be consuming all mouse events and not
the JEditorPane)
Has anybody tried anything similar? And succeeded??
|
|
0
|
|
|
|
Reply
|
austinonthenet (2)
|
1/21/2005 6:00:05 AM |
|
On 1/21/2005 at 1:00:05 AM, Austin wrote:
> I would like to render text inside JTable column cells as Hyperlinks
> (so the hand-cursor appears when the mouse hovers over the cells, and a
> single click event on the link can be trapped.
>
> I tried using JEditorPane as the cellrenderer for the JTable column
> with a HyperLinkListener, but it doesn't seem to work - although the
> text appears as a hyperlink, mouse hover over the cell does not change
> the cursor.
That is because the CellRenderer is not there when your mouse hovers over
the cell. A renderer is not a realized component. It is (sort of) placed
over the cell when the cell needs painting, and then is immediately
whisked away. Renderers cannot respond to events on the cell.
If you want to do any event handling, without a TableCellEditor being
active, you will need to listen for the events on the Table itself, and
respond to them appropriately. So to handle hyperlinks, you would need to
use a MouseMotionListener on the JTable, detect if you are over a link,
change the cursor yourself, etc.
Are you sure you want to use a JTable for this?
--
Regards,
John McGrath
|
|
0
|
|
|
|
Reply
|
John
|
1/21/2005 6:32:53 PM
|
|
Austin wrote:
> I would like to render text inside JTable column cells as Hyperlinks
> (so the hand-cursor appears when the mouse hovers over the cells, and a
> single click event on the link can be trapped.
Sure, it is easy. And most likely you don't want to use JEditorPane.
You need:
- LinkListener extends MouseAdapter
- LinkRenderer extends DefaultTableCellRenderer
LinkRenderer {
setValue(Object pValue) {
boolean isLinkValue = ...
if (isLinkValue) {
String link = "<html>"
+ "<a href=\"x\">" + pValue.toString() + "</a>"
+ "</html>";
setText(link);
putClientProperty(KEY_LINK, pValue);
} else {
...
}
}
}
LinkListener {
mouseOver() {
renderer = ...
LinkRenderer c = renderer.getComponent(...);
Object value = c.getClientProperty(KEY_LINK);
if (value!=null) {
setMouseCursor(CURSOR_HAND);
} else {
setMouseCursor(CURSOR_DEFAULT);
}
}
mouseClicked() {
renderer = ...
LinkRenderer c = renderer.getComponent(...);
Object value = c.getClientProperty(KEY_LINK);
if (value!=null) {
activateLink(..., value);
}
}
}
Doing thing so that link is rendered only when mouse is hovering over label
is not much more difficult. Also handling multiple links from single cell
is also feasible. Some minor things are needed to adjust HTML linkstyle to
match normal cell renderer font style (for JDK 5.0).
|
|
0
|
|
|
|
Reply
|
Kari
|
1/22/2005 10:32:47 AM
|
|
Kari Ikonen wrote:
> Austin wrote:
>
>
>>I would like to render text inside JTable column cells as Hyperlinks
>>(so the hand-cursor appears when the mouse hovers over the cells, and a
>>single click event on the link can be trapped.
>
>
> Sure, it is easy. And most likely you don't want to use JEditorPane.
>
> You need:
>
> - LinkListener extends MouseAdapter
> - LinkRenderer extends DefaultTableCellRenderer
>
>
> LinkRenderer {
> setValue(Object pValue) {
> boolean isLinkValue = ...
> if (isLinkValue) {
> String link = "<html>"
> + "<a href=\"x\">" + pValue.toString() + "</a>"
> + "</html>";
> setText(link);
> putClientProperty(KEY_LINK, pValue);
> } else {
> ...
> }
> }
> }
>
>
> LinkListener {
> mouseOver() {
> renderer = ...
> LinkRenderer c = renderer.getComponent(...);
> Object value = c.getClientProperty(KEY_LINK);
> if (value!=null) {
> setMouseCursor(CURSOR_HAND);
> } else {
> setMouseCursor(CURSOR_DEFAULT);
> }
> }
>
> mouseClicked() {
> renderer = ...
> LinkRenderer c = renderer.getComponent(...);
> Object value = c.getClientProperty(KEY_LINK);
> if (value!=null) {
> activateLink(..., value);
> }
> }
> }
>
>
> Doing thing so that link is rendered only when mouse is hovering over label
> is not much more difficult. Also handling multiple links from single cell
> is also feasible. Some minor things are needed to adjust HTML linkstyle to
> match normal cell renderer font style (for JDK 5.0).
>
Are you sure this will work? I thought that mouse/keyboard events were
not passed to the rendered components, only too edited components.
|
|
0
|
|
|
|
Reply
|
Andrew
|
1/22/2005 11:09:03 AM
|
|
Andrew McDonagh wrote:
> Are you sure this will work? I thought that mouse/keyboard events were
> not passed to the rendered components, only too edited components.
Of course it works.
LinkListener is attached to JTable, there is not any kind of editor
component involved here. Trick is to find out renderer for cell, let it
render value and then just check this rendered value.
you need to:
- resolve (column, row) from mouse coordinates
- get renderer for (column, row)
(- and not mix up table vs. model column indexes)
|
|
0
|
|
|
|
Reply
|
Kari
|
1/22/2005 5:02:50 PM
|
|
Kari Ikonen wrote:
> Andrew McDonagh wrote:
>
>
>>Are you sure this will work? I thought that mouse/keyboard events were
>>not passed to the rendered components, only too edited components.
>
>
> Of course it works.
>
> LinkListener is attached to JTable, there is not any kind of editor
> component involved here. Trick is to find out renderer for cell, let it
> render value and then just check this rendered value.
>
> you need to:
> - resolve (column, row) from mouse coordinates
> - get renderer for (column, row)
> (- and not mix up table vs. model column indexes)
>
Oh I see, you are adding your mouse listener directly to the JTable...
I thought you were adding it to the component rendered by the renderer.
In that case, yes I agree, it would work as you say.
|
|
0
|
|
|
|
Reply
|
Andrew
|
1/22/2005 6:07:52 PM
|
|
Thank you all very much, especially Kari!
|
|
0
|
|
|
|
Reply
|
Austin
|
1/27/2005 5:02:31 AM
|
|
|
6 Replies
363 Views
(page loaded in 0.088 seconds)
|