Java JButton Settings equalizeButtonSizes(JPanel jPanelButtons)

Here you can find the source of equalizeButtonSizes(JPanel jPanelButtons)

Description

Sets the JButtons inside a JPanelto be the same size.

License

Apache License

Parameter

Parameter Description
jPanelButtons JPanel containing buttons

Declaration

public static void equalizeButtonSizes(JPanel jPanelButtons) 

Method Source Code


//package com.java2s;
/*//from   w w  w  .  ja va 2s .  co  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.swing.*;
import java.util.ArrayList;
import java.awt.*;

public class Main {
    /**
     * Sets the JButtons inside a JPanelto be the same size.
     * This is done dynamically by setting each button's preferred and maximum
     * sizes after the buttons are created. This way, the layout automatically
     * adjusts to the locale-specific strings.
     *
     * @param jPanelButtons JPanel containing buttons
     */
    public static void equalizeButtonSizes(JPanel jPanelButtons) {
        ArrayList<JButton> lbuttons = new ArrayList<JButton>();
        for (int i = 0; i < jPanelButtons.getComponentCount(); i++) {
            Component c = jPanelButtons.getComponent(i);
            if (c instanceof JButton) {
                lbuttons.add((JButton) c);
            }
        }

        // Get the largest width and height
        Dimension maxSize = new Dimension(0, 0);
        for (JButton lbutton : lbuttons) {
            Dimension d = lbutton.getPreferredSize();
            maxSize.width = Math.max(maxSize.width, d.width);
            maxSize.height = Math.max(maxSize.height, d.height);
        }

        for (JButton btn : lbuttons) {
            btn.setPreferredSize(maxSize);
            btn.setMinimumSize(maxSize);
            btn.setMaximumSize(maxSize);
        }
    }
}

Related

  1. doSetText(final AbstractButton abstractButton, final String text)
  2. doSetTextInEDT(final AbstractButton abstractButton, final String text)
  3. drawActiveButtonBorder(Graphics g, int x, int y, int w, int h)
  4. drawDefaultButtonBorder(Graphics g, int x, int y, int w, int h, boolean active)
  5. emphasizeButton(AbstractButton btn)
  6. filterMnemonic(final String s, final AbstractButton b)
  7. findButton(Container container, String text)
  8. findButtonComponents(Container container, String label)
  9. findDescendantButtonWithText(Container root, String desiredText)