Home arrow static arrow Java Programming [Archive] - How to add a mouselistener on a TitledBorder
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - How to add a mouselistener on a TitledBorder
10 Duke Stars available
This topic has 4 replies on 1 page.

Posts:18
Registered: 3/5/04
How to add a mouselistener on a TitledBorder  
Aug 3, 2004 7:55 AM



 
Hi,

I have a challenging problem:

I have a JPanel with a border around it which i set with setBorder(new TitledBorder("test")).

How can i get a mouselistener on this border? Not on the panel but only on the word "test " of the border?

Which genius can solve this problem?

Thanks
 

Posts:31,095
Registered: 4/30/99
Re: How to add a mouselistener on a TitledBorder  
Aug 3, 2004 8:21 AM (reply 1 of 4)



 
Most likely somebody in the Swing forum.
 

Posts:14,142
Registered: 99-04-02
Re: How to add a mouselistener on a TitledBorder  
Aug 3, 2004 11:01 AM (reply 2 of 4)



 
The border is not a component, it's just painted on the component. So what you could is create a class that implements MouseListener and registers with the component in question. Then you get the component's border information and compare that to the mouse event's location and determine if the event is within the border and do whatever you want if that is the case.
 

Posts:14,142
Registered: 99-04-02
Re: How to add a mouselistener on a TitledBorder  
Aug 3, 2004 11:02 AM (reply 3 of 4)



 
BTW, you can use the FontMetrics class to get the string size for the title string based on the font used for the title border.
 

Posts:1,025
Registered: 04-03-17
Re: How to add a mouselistener on a TitledBorder  
Aug 3, 2004 11:55 AM (reply 4 of 4)



 
Hi,

I have a challenging problem:

I have a JPanel with a border around it which i set
with setBorder(new TitledBorder("test")).

How can i get a mouselistener on this border? Not on
the panel but only on the word "test " of the border?

Which genius can solve this problem?

I'm not sure what your attempting, but some time ago, this guy named Nobuo Tamemasa coded a bunch of cool things that you can do with Swing. Genius? Perhaps. Anyway, the original link went down, but [url=http://www.physci.org/codes/tame/index.jsp]this website[/url] I believe is attempting to re-write them for current Swing compatibility, since the originals were written back in 1999. I've tried the JRadioButton as a TitledBorder and it works just as I wanted it to (really nice). Perhaps this could be a starting point for you since it involves the concept of mouseclicks on a TitledBorder.

However, as I'm still learning Swing, I read somewhere on Java Swing Tutorial that you should never call one of the paint methods that he's using (not sure why - thread safety?). Anyway, perhaps fixing these "errors" are what these people a physci.org are trying to do. Again, I'm not sure.

Good luck.
 
This topic has 4 replies on 1 page.