Java Utililty Methods JButton Size

List of utility methods to do JButton Size

Description

The list of methods to do JButton Size are organized into topic(s).

Method

voidadjustSize(JButton button)
adjust Size
button.setPreferredSize(new Dimension(0, BUTTON_HEIGHT));
button.setMaximumSize(new Dimension(Integer.MAX_VALUE, BUTTON_HEIGHT));
voidequalizeSize(JButton[] aButtons)
Resizes the given buttons making them equal in size.
final Dimension preferredSize = aButtons[0].getPreferredSize();
final Dimension maximumSize = aButtons[0].getMaximumSize();
for (int i = 1; i < aButtons.length; ++i) {
    ensureSize(preferredSize, aButtons[i].getPreferredSize());
    ensureSize(maximumSize, aButtons[i].getMaximumSize());
setSizes(aButtons, preferredSize, maximumSize);
voidmakeButtonsSameSize(JButton[] buttons)
make Buttons Same Size
if (buttons.length < 2) {
    return; 
Dimension size = buttons[0].getPreferredSize();
int maxWidth = size.width;
int maxHeight = size.height;
int i;
for (i = 1; i < buttons.length; i++) {
...
voidScaleButtonIcon(javax.swing.JButton btn, int width, int height, int fontsize)
Scale Button Icon
btn.setMargin(null);
int iconWidth = width;
int iconHeigth = height;
if (btn.getText() != null && !btn.getText().isEmpty()) {
    Font font = btn.getFont();
    btn.setFont(new Font(font.getName(), font.getStyle(), fontsize));
    FontMetrics fm = btn.getFontMetrics(font);
    btn.setHorizontalTextPosition(JButton.CENTER);
...
voidsetButtonSize(JButton button, Dimension size)
set Button Size
String text = button.getText();
button.setMinimumSize(size);
if (text.length() == 0) {
    button.setPreferredSize(size);
} else {
    Dimension psize = button.getPreferredSize();
    if (psize.width < size.width)
        button.setPreferredSize(size);
...
voidsetButtonSize(JButton button, int width, int height)
Convenience method that sets both the preferred and maximum size of a JButton.
Dimension bDim = new Dimension(width, height);
button.setPreferredSize(bDim);
button.setMaximumSize(bDim);
voidsetJButtonSizesTheSame(final JButton[] btns)
set J Button Sizes The Same
if (btns == null) {
    throw new IllegalArgumentException();
final Dimension maxSize = new Dimension(0, 0);
for (int i = 0; i < btns.length; ++i) {
    final JButton btn = btns[i];
    final FontMetrics fm = btn.getFontMetrics(btn.getFont());
    final Rectangle2D bounds = fm.getStringBounds(btn.getText(), btn.getGraphics());
...
voidsetJButtonSizesTheSame(JButton[] btns)
Change the sizes of all the passed buttons to be the size of the largest one.
if (btns == null) {
    throw new IllegalArgumentException("null JButton[] passed");
final Dimension maxSize = new Dimension(0, 0);
for (int i = 0; i < btns.length; ++i) {
    final JButton btn = btns[i];
    final FontMetrics fm = btn.getFontMetrics(btn.getFont());
    Rectangle2D bounds = fm.getStringBounds(btn.getText(), btn.getGraphics());
...
voidsetSizes(JButton[] aButtons, Dimension aPreferred, Dimension aMax)
Sets the preferred and maximum sizes of the given buttons.
for (final JButton button : aButtons) {
    button.setPreferredSize(aPreferred);
    button.setMaximumSize(aMax);
JButtonSetStandardSizeForButton(JButton btn)
Set Standard Size For Button
btn.setMaximumSize(new Dimension(95, 95));
btn.setMinimumSize(new Dimension(95, 95));
btn.setHorizontalTextPosition(SwingConstants.CENTER);
btn.setVerticalTextPosition(SwingConstants.BOTTOM);
btn.setFont(new Font("Arial", Font.PLAIN, 10));
return btn;