JButton, preferredSize
Hi,
I have a JPanel and a JButton; the JPanel contains an image (64x64px), the JButton another image (16x16px). I am using Borderlayout to arrange them:
JPanel wDPanel =new JPanel();
JLabel cLabel =new JLabel(cIcon64x64);
infoButton =new JButton(infoIcon16x16);
infoButton.setPreferredSize(new Dimension(20, 20));
wDPanel.setLayout(new BorderLayout());
wDPanel.add(cLabel, BorderLayout.CENTER);
wDPanel.add(infoButton, BorderLayout.PAGE_END);
The JButton is being stretched to the size of the JPanel which has a width of about 200px. Is there a way to set the button to around the size of the image it contains (16x16, so maybe 20x20px)?
Thanks!
Edit: instead of using such a layout... could I use a JToolbar in a JPanel?
Message was edited by:
SFL
[1002 byte] By [
SFLa] at [2007-11-27 2:29:13]

# 2
> Edit: instead of using such a layout... could I use a
> JToolbar in a JPanel?
>
I think you can use a JToolBar. But setFloatable(0) so that it's fixed. And if you want to make the JButton the size of the image (or close to it), play with the Insets (if I recall correctly). However, as mentioned in the other post, you can just use a JPanel() and add it where you are adding the JButton now. You can also "force" the size of the JButton by doing something like:
Dimension d = new Dimension(16, 16);
yourbutton.setPreferredSize(d);
yourbutton.setMinimumSize(d);
yourbutton.setMaximumSize(d);
# 3
OK, i am using now a JPanel for the JButton, the layout works better now. I tried to use BoxLayout but it's not possible: "BoxLayout can't be shared".
I am playing now with GridBagLayout...
BTW: I am using 2 JPanels, one for the image and one for the button; it seems that there is a great gap / spacing between the JPanels and their borders...
SFLa at 2007-7-12 2:42:10 >

# 4
[nobr]If you want a button with an image, just use html tags inside the JButton's label. Then you won't have trouble with layouts.
Something like:
JButton mybutton = new JButton("<html><center><img src=somefile.gif><br>Test</center></html>")
Untested, but you can test for yourself..[/nobr]