0

I’m currently writing a custom TableCellRenderer using a JEditorPane as cell-component for a JTable. I want to make hyperlinks clickable in that component.

Therefore, I wrote a tiny hack that forwards mouse events that are fired on the Table to the specific component. The problem is, that I cannot use the viewToModel function of the JEditorPane to determine the text under the mouse cursor, because the size of my editor pane is always zero and therefore viewToModel always returns -1.

Is there any way to get the components rendered in the table’s cells to have their correct sizes set? Do you have any suggestions on doing it in another way?

Some side information: I cannot use JTextPane since I'm using the scala programming language and there is currently no implementation for it. Furthermore, I have read that it may be possible to use the native event implementation by using custom cell editors. But I could not get this working in the first place. I want that, for instance, links are highlighted by hovering over. But, as far as I can see, that is not possible with the cell editor approach, since you have to click at the cell to start editing first.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Manuel
  • 21
  • 5
  • You could use a HyperlinkListener instead of messing with things like `viewToModel`. [Example](http://stackoverflow.com/questions/14170041/is-it-possible-to-create-programs-in-java-that-create-text-to-link-in-chrome/14170141#14170141). – DSquare Aug 08 '14 at 15:45
  • The problem with this is, there are no events delivered to the component. Thats why I had to manually forward the mouse events from the table to the component. How would you accomplish using a HyperlinkListener then? – Manuel Aug 08 '14 at 15:57
  • Sure, you will need to forward some events to the inner components, but once the appropiate mouse events reach them the inner listeners should work on their own. Specifically you will also need to forward the MouseEvents that a MouseMotionListener handles. – DSquare Aug 08 '14 at 16:04
  • Do the listening in your custom `CellEditor`, or use an adjacent component, for [example](http://stackoverflow.com/a/25170471/230513). – trashgod Aug 08 '14 at 16:33
  • Im currently working on DSquare's attempt. Not sure if this works cause the size of the component is still zero. I don't understand how a custom editor will help though. How will it handle mousemoves without clicking on the cell? I don't like the idea of an adjacent component since I'm writing a twitter client and want to list the tweets in the table. – Manuel Aug 08 '14 at 17:20
  • Ok, so I forwarded every mouse related event to the specific component. As expected, the HyperLinkListener does not receiv a hyperlik event. I guess this is caused by the zero size of the component. Furthermore, I get exceptions when I, for instance, double click anywhere in the text because the component tries to figure out the text under the mouse ("viewToModel" function), which is -1 caused by the zero size aswell. So the initial problem remains. – Manuel Aug 08 '14 at 18:10
  • @user3037873 try to implement the editor which looks like the renderer and set the click count to 1. Event will be forwarded automatically and the editor will have a non-zero size. So you can use the HyperlinkListener to monitor the clicks. The editor can be finished automatically after the click has been processed. – Sergiy Medvynskyy Aug 08 '14 at 19:20
  • Ok, I implemented a custom CellEditor, that looks just like the renderer. I still don't get how MouseMove and click events can be forwarded automaticall without clicking on the Cell first. Maybe I can now implement the manual forwarding, since the size of the cell is now correctly set in the editor. @Sergiy Medvynskyy Can u explain ur approach a bit more? Especially the part with the click count? – Manuel Aug 12 '14 at 16:20
  • @user3037873 you can use the click count if you extend the class DefaultCellEditor. If not you can simply implement the method CellEditor.isCellEditable(EventObject anEvent) which should always return true in your case. If the event can trigger editing the editor component will be initialized and this event will be forwarded on editor component automatically. For example this works when you define an editable column as boolean column in table model. In this case table provide the editor (check box) automatically. – Sergiy Medvynskyy Aug 12 '14 at 18:31
  • I override the isCellEditable method, but sadly that doesn't change anything. I still have to click the cell first before I can click the actual hyperlinks. Of course I had to implement the AbstractCellEditor with TableCellEditor because I use a JEditorPane as component which is not possible with the DefaultCellEditor as far as I know. – Manuel Aug 16 '14 at 16:33

0 Answers0