How can i display a image on a small jbutton.

I am trying to create a minesweeper game. I have used jb.setBounds(0,0,20,20) on the jbuttons in the minesweeper field.

To display the image of the flag I use ...

jb.setIcon( new ImageIcon( "flag.gif" ) );

and to display numbers i use ...

jb.setText( "" + i );// i is an integer variable.

But I am not able to display either the image or the text. All that is displayed in both the cases are 3 small dots near the bottom of the jbutton in minesweeper field. When i use a bigger size for the jbuttons, I have no problem. Is there a way to get around this problem.

I am only a novice, so if I am using a wrong approach or u have a better approach please suggest it to me. Please also suggest someway to keep the size of grid constant in different resolutions.

[803 byte] By [RDXa] at [2007-11-26 18:05:34]
# 1
The "..." appear because the text is too big. Your options are:a) use a smaller fontb) use a larger buttonc) maybe add an EmptyBorder to the button
camickra at 2007-7-9 5:36:13 > top of Java-index,Desktop,Core GUI APIs...
# 2

> All that is displayed in both the cases are 3 small dots

1) get rid of the margin

button.setMargin(new Insets(0,0,0,0));

2) change to a smaller font size

button.setFont(button.getFont().deriveFont(8f));

> Please also suggest someway to keep the size of grid constant in different resolutions.

use a layout manager

Michael_Dunna at 2007-7-9 5:36:13 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thanks a lot guys!

Using setMargin() alone solved my problem.

But will you elaborate how using a layout manager will help in keeping the grid size constant. I am using GridBagLayout() to construct the grid. But the size of grid depends on the size of the buttons it is made of. And i have to use number of pixels to specify the size of buttons e.g. setPreferredSize(20,20).

Is there a way to specify size in physical units like cms, inches, etc.

RDXa at 2007-7-9 5:36:13 > top of Java-index,Desktop,Core GUI APIs...
# 4
Is there a way to specify size of components in physical units like cms, inches, etc.
RDXa at 2007-7-9 5:36:13 > top of Java-index,Desktop,Core GUI APIs...
# 5
> Is there a way to specify size in physical units like cms, inches, etc. No.
camickra at 2007-7-9 5:36:13 > top of Java-index,Desktop,Core GUI APIs...
# 6
Yes, probably :o)See this thread, mainly reply 5. http://forum.java.sun.com/thread.jspa?forumID=57&threadID=774916
itchyscratchya at 2007-7-9 5:36:13 > top of Java-index,Desktop,Core GUI APIs...