• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Setting default size to JToolBar's Buttons

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dalia Sultana
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));

}

}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!