This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I'm building a custom component  RSS feed

 
shawn larkin
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm experimenting with building a custom component. I'm having pretty good sucess, the only thing that's left is having the component set it's size based on the text it holds... just like the JButton. I've been looking through the src zip file at the different APIs like ComponentUI ect, but I can't figure out how JButton does it. Can anyone help?
thanks
 
jacq carballo
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What types of components are you planning to use? Maybe setting or using layout managers could help.
 
shawn larkin
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm overriding the paintComponent() method, and drawing a String on top of a rectangle. What I want to do is have this component size itself based on the width of the String that gets draw on top of the rectangle just like the JButton does. Any Ideas?
 
vinsil reno
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, you can use sthg like this (using FontMetrics):
Assuming that CustomComponent extends Component and that you've got a ref to customComponent (an instance of CustomComponent) :

it *should* work...
 
shawn larkin
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!