{"id":3078,"date":"2013-06-19T21:40:30","date_gmt":"2013-06-19T21:40:30","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=3078"},"modified":"2015-12-21T00:10:05","modified_gmt":"2015-12-21T00:10:05","slug":"how-to-obtain-the-coordinates-of-the-jtable-cell-a-mouse-is-hovering-over-useful-for-tooltips-within-cell-renderers","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/how-to-obtain-the-coordinates-of-the-jtable-cell-a-mouse-is-hovering-over-useful-for-tooltips-within-cell-renderers\/","title":{"rendered":"How to obtain the coordinates of the JTable cell a mouse is hovering over, useful for tooltips within cell renderers."},"content":{"rendered":"<pre><code>    public String getToolTipText(MouseEvent event) {\n        \/*\n         * This is a Java Swing lesson on how to obtain the coordinates of the current cell\n         * as you hover with the mouse on a JTable.\n         * \n         * You cannot use the renderer component, since it seems that once the table is done\n         * stamping the cells with it, the instance of the renderer is not the one under the mouse\n         * as it will always yield negative coordinates, for example, our debugger showed that this\n         * renderer's coordinates were always (-95,-25)...\n         * \n         * What we did in this case, to show labels for the specific components inside the renderer\n         * was to get the mouse coordinates, and translate its coordinates to the coordinates of the\n         * current Cell Rectangle.\n         * \n         * One interesting gotcha in the process is that you cannot alter the event coordinates and then\n         * try to use event.getPoint() since event.getPoint() will always give you a new instance of Point\n         * so we keep a copy of that Point and then translate that point.\n         * \n         * tags: java, swing, table, get current cell coordinates, get table cell coordinates under mouse\n         *\/\n        try {\n            Component[] components = this.getComponents();\n            Point p = event.getPoint();\n            int row = table.rowAtPoint(p);\n            int col = table.columnAtPoint(p);\n            Rectangle currentCell = table.getCellRect(row, col, false);\n            p.translate(-currentCell.x, -currentCell.y);\n\n            for (Component c : components) {\n                JComponent jc = (JComponent) c;\n                if (jc.isVisible() &amp;&amp; jc.getBounds().contains(p)) {\n                    return jc.getToolTipText(event);\n                }\n            }\n        } catch (Throwable t) {\n            \/\/don't risk painting the table over a tooltip\n        }\n        return super.getToolTipText(event);\n    }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>public String getToolTipText(MouseEvent event) { \/* * This is a Java Swing lesson on how to obtain the coordinates of the current cell * as you hover with the mouse on a JTable. * * You cannot use the renderer component, since it seems that once the table is done * stamping the cells with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[1279,1280,583,1177,1176],"class_list":["post-3078","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-cell-rectangles","tag-cell-renderers","tag-java","tag-jtable","tag-swing"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-NE","jetpack-related-posts":[{"id":2142,"url":"https:\/\/www.gubatron.com\/blog\/how-to-convert-android-gps-coordinates-into-xy-coordinates\/","url_meta":{"origin":3078,"position":0},"title":"How to convert Android GPS coordinates into X,Y coordinates.","author":"gubatron","date":"December 30, 2010","format":false,"excerpt":"Without further math bullshit about all the conversion systems, when you have a bunch of Android GPS coordinates (which are compatible with Google Earth and Google Maps), and you want to draw them on a finite 2D plane, here's what worked for me. [java] int x = (int) ((PLANE_WIDTH\/360.0) *\u2026","rel":"","context":"In &quot;Android&quot;","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":387,"url":"https:\/\/www.gubatron.com\/blog\/another-reason-why-i-hate-ie-getting-mouse-coordinates\/","url_meta":{"origin":3078,"position":1},"title":"Another Reason why I hate IE &#8211; Getting Mouse Coordinates","author":"gubatron","date":"October 29, 2006","format":false,"excerpt":"Fucking IE!!! If I had a dollar for everytime I say that. In anycase, I just fixed a bug which has been keeping all the people that visit wedoit4you.com from recommending articles by mail to their friends. It has to do with getting the current position of the mouse. Every\u2026","rel":"","context":"In &quot;Geeklife&quot;","block_context":{"text":"Geeklife","link":"https:\/\/www.gubatron.com\/blog\/category\/geeklife\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1114,"url":"https:\/\/www.gubatron.com\/blog\/using-a-linear-array-as-a-bidimensional-matrix\/","url_meta":{"origin":3078,"position":2},"title":"Using a linear array as a bidimensional matrix","author":"gubatron","date":"January 27, 2009","format":false,"excerpt":"Often times I find the need to use a list or linear array as if it was a table. Everytime I need to do so, I always end up coding functions to convert a (x,y) coordinate to the real index n in the array. Let me illustrate, with an example.\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2126,"url":"https:\/\/www.gubatron.com\/blog\/animating-a-game-like-sky-with-html5-canvas\/","url_meta":{"origin":3078,"position":3},"title":"Animating a game-like sky with HTML5 Canvas","author":"gubatron","date":"December 28, 2010","format":false,"excerpt":"Try it | View Source Again playing a little more with HTML5 and Canvas animation. This time around the result is a little more pleasing to the eye, based on some ideas I have for a little arcade game I want to make I've created a gradient blue sky and\u2026","rel":"","context":"In &quot;AJAX&quot;","block_context":{"text":"AJAX","link":"https:\/\/www.gubatron.com\/blog\/category\/ajax\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":590,"url":"https:\/\/www.gubatron.com\/blog\/emacs-pocket-guide\/","url_meta":{"origin":3078,"position":4},"title":"Emacs Pocket Guide","author":"gubatron","date":"September 12, 2007","format":false,"excerpt":"I wrote this a long long time ago when I was learning emacs, then pasted it on linuxmachos.org, but that site is down atm. So here's for future reference EMACS POCKET GUIDE Ctrl+g Cancels anything (useful for n00bs) == Saving == Ctrl+x s Save buffer (buffer a.k.a filez) Ctrl+x Ctrl+w\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2505,"url":"https:\/\/www.gubatron.com\/blog\/raw-notes-from-singularity-summit-ss11\/","url_meta":{"origin":3078,"position":5},"title":"Raw Notes from Singularity Summit #SS11","author":"gubatron","date":"October 21, 2011","format":false,"excerpt":"Singularity Summit 2011 Notes. The #SS11 videos are going to be at http:\/\/vimeo.com\/siai Day 1: Kurzweil: > TO-POST: Post about the the continuity of consciousness. Watson is coded in Java, was built for massive parallelization with support for internet parallelization, and runs on linux. Creating horizontal AI solutions is key\u2026","rel":"","context":"In &quot;Singularity&quot;","block_context":{"text":"Singularity","link":"https:\/\/www.gubatron.com\/blog\/category\/singularity\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=3078"}],"version-history":[{"count":2,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3078\/revisions"}],"predecessor-version":[{"id":3412,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3078\/revisions\/3412"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=3078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=3078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=3078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}