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?
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.
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); } }