Forums Register Login

Setting default size to JToolBar's Buttons

+Pie Number of slices to send: Send
Hi All,

I am trying to set a default size of all buttons in a JToolBar. I have created a class and overriden the getPreferredSize() methos like this -

public class ToolBarButton extends JButton {
....
public Dimension getPreferredSize() {
return new Dimension(100,super.getPreferredSize().height);
}

}

the buttons have no captions but they have icons. After adding to the Toolbar, I see that they are not getting the size that I defined in gerPrefferedSize method. Any ideas?
+Pie Number of slices to send: Send
Testing shows that the size of the buttons increases up to 99 and then they collapse to
their super.getPreferredSize.width for all larger widths of the toolBar. The layout manager
is the private class javax.swing.JToolBar$DefaultToolBarLayout which uses a BoxLayout. So it
looks like you could try increasing the preferred width or setting your own layout manager.
+Pie Number of slices to send: Send
Seems like I had to set the maximum and minimum size of the button as well. I ran into something else however. Instead of overriding the getPrefferedSize(). getMaximumSize() etc, I tried setting the prefferedSize, max size and min size of the button, which strangely produced different result. It seemed like the height of the buttons were bigger then they needed to be. So button.setPrefferedSize(..) is somehow different then overriding the getPrefferedSize() method in the button class. Does anyone know why?

To make things clearer, this is my question, what is the difference between the following pieces of code?

1)
public class ToolBarButton extends JButton{
....
public Dimension getMaximumSize() {
return new Dimension(65,super.getPreferredSize().height);

}
@Override
public Dimension getMinimumSize() {
return new Dimension(65,super.getPreferredSize().height);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(65,super.getPreferredSize().height);
}
}

2) public class ToolBarButton extends JButton{

public ToolBarButton(DefaultAction a,char mnemonic,boolean enabled) {
super(a);
setMnemonic(mnemonic);
setIcon(a.icon);
setEnabled(enabled);
this.setPreferredSize(new Dimension(65,super.getPreferredSize().height));
this.setMaximumSize(new Dimension(65,super.getPreferredSize().height));
this.setMinimumSize(new Dimension(65,super.getPreferredSize().height));

}

}
Your mother was a hamster and your father was a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 6812 times.
Similar Threads
Text canvas vertical scroll
How can I autodraw with custom JComponent?
repaint paints the wrong component
placing image on jpanel and then add other components
Trouble Aligning JButtons (And setting width)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:04:31.