Java Utililty Methods JButton Settings

List of utility methods to do JButton Settings

Description

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

Method

voidequalizeButtonSizes(JPanel jPanelButtons)
Sets the JButtons inside a JPanelto be the same size.
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);
Dimension maxSize = new Dimension(0, 0);
...
StringfilterMnemonic(final String s, final AbstractButton b)
Filters the mnemonic definition from the given string.
if (s != null) {
    String tmpS = s;
    final Matcher m = Pattern.compile("\\[(.)\\]").matcher(tmpS); 
    if (m.find()) {
        b.setMnemonic(m.group(1).charAt(0));
        tmpS = m.replaceFirst(m.group(1));
    return tmpS;
...
JButtonfindButton(Container container, String text)
Traverse a container hierarchy and returns the button with the given text
Component[] components = container.getComponents();
for (Component component : components) {
    if (component instanceof JButton) {
        JButton button = (JButton) component;
        if (button.getText().equals(text)) {
            return button;
    } else if (component instanceof Container) {
...
JButtonfindButtonComponents(Container container, String label)
find Button Components
for (Component c : container.getComponents()) {
    if (c instanceof JButton) {
        final JButton button = (JButton) c;
        if (button.getText().equalsIgnoreCase(label))
            return button;
return null;
...
AbstractButtonfindDescendantButtonWithText(Container root, String desiredText)
Find a descendant button in the container root that has a getText() method that returns the given text.
Component[] children = root.getComponents();
for (int i = 0; i < children.length; ++i) {
    if (children[i] instanceof AbstractButton) {
        AbstractButton button = (AbstractButton) children[i];
        if (button.getText().equals(desiredText)) {
            return button;
for (int i = 0; i < children.length; ++i) {
    if (children[i] instanceof Container) {
        AbstractButton button = findDescendantButtonWithText((Container) children[i], desiredText);
        if (button != null) {
            return button;
return null;
voidfixButtonUI(AbstractButton button)
Ensures that focus will be really painted if button is focused and fixes using custom border for JDK 1.5 & XP LaF
if (button.getUI() instanceof com.sun.java.swing.plaf.windows.WindowsButtonUI) {
    button.setUI(new com.sun.java.swing.plaf.windows.WindowsButtonUI() {
        protected BasicButtonListener createButtonListener(AbstractButton b) {
            return new BasicButtonListener(b); 
        protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect,
                Rectangle iconRect) {
            int width = b.getWidth();
...
JButtonformatButtonFromAction(Action a)
Creates a new button and formats it using settings from action.
JButton button = new JButton();
button.setToolTipText((String) a.getValue(Action.SHORT_DESCRIPTION));
button.setIcon((Icon) a.getValue(Action.SMALL_ICON));
return button;
AbstractButtongetAbstractButton(Container owner, String text)
get Abstract Button
for (Component c : owner.getComponents()) {
    if (c instanceof AbstractButton && ((AbstractButton) c).getText() != null
            && ((AbstractButton) c).getText().equals(text))
        return (AbstractButton) c;
    else if (c instanceof JComponent) {
        AbstractButton b = getAbstractButton((JComponent) c, text);
        if (b != null)
            return b;
...
ListgetAllButtons(Component topComponent)
Returns an array with all JButton components contained in a top component.
List<Component> components = getAllComponents(topComponent);
List<JButton> buttons = new ArrayList<>();
for (Component component : components) {
    if (component instanceof JButton) {
        buttons.add((JButton) component);
return buttons;
...
JButtongetbtnButton(String iconpath, String rolloverIconpath, String pressIconpath)
getbtn Button
JButton button = new JButton();
button.setIcon(new ImageIcon(iconpath));
button.setRolloverIcon(new ImageIcon(rolloverIconpath));
button.setPressedIcon(new ImageIcon(pressIconpath));
button.setBorder(null);
button.setFocusPainted(false);
button.setContentAreaFilled(false);
return button;
...