Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JButton with icon as background and button name as pop up

 
david arnold
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to create a few buttons vertically on right side of a window, these buttons take images/icons as background, but do not show button name, only when mouse is moved to the button, button name will be shown beside the mouse, like those icons on the tool bar. And when a checkbox is clicked, these button can be disabled. Could I use Jbutton to do this? How?

Thanks.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a JButton can do this. You can give any Swing JComponent a tool tip, and I suggest you study the tool tip section of the Sun Swing tutorial for more information. Also, to disable /enable a button or any JComponent, simply call its setEnabled(boolean enabled) method.

how to use tooltips
 
david arnold
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, will do it. Thank you Pete!

One more question, seems the button size is affected by the icon size when I use . Since icons have different sizes, but I want all button to have same size, seems it does not work even I use method. Any idea? Should I create new icons that have same size?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tony xue wrote:oh, will do it. Thank you Pete!

You're welcome!


One more question, ...
Any idea? Should I create new icons that have same size?


One possible solution: If your JButtons are held in a JPanel that uses GridLayout, they'll all be the same size. For example, if the JButtons are held horizontally, then using a GridLayout(0, 1, 0, vGap) could work well where the parameters mean it will have a variable number of rows, one column, and a vertical spacing between JButtons of vGap size.
 
david arnold
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Pete. I am using BoxLayout, since the buttons only occupy upper part the panel, there is blank space below buttons. If I use GridLayout as you told, I guess I need create another smaller panel in which the buttons will occupy the whole panel and get same size, I will think about it. Anyway, thank again for the prompt help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic