• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting default size to JToolBar's Buttons

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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));

}

}
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic