Soler Mani
Soler Mani

Reputation: 309

JButtons to adjust to size based on it's textual contents

I am working on a project which requires creating a number of JButtons. Each of these buttons will enclose different characters; a few of which containing words. The problem is I cannot seem to find out how I can set a JButton to resize based on its textual contents.

Is there a method which does this?

Thanks for any help.

Upvotes: 2

Views: 2459

Answers (1)

alex
alex

Reputation: 1229

JButton.getPreferredSize() will give you the size required to display the text and icon. The Button will automatically update it's preferred size if you change the text/icon.

If the button is actually rendered in with its preferred size depends on the LayoutManager of its container.

Upvotes: 4

Related Questions