Java JButton Settings decoratedToSimpleButton(final T button)

Here you can find the source of decoratedToSimpleButton(final T button)

Description

Set the button to have simplified UI.

License

Apache License

Parameter

Parameter Description
button button to be modified
T type of button

Return

button

Declaration

public static <T extends AbstractButton> T decoratedToSimpleButton(final T button) 

Method Source Code


//package com.java2s;
/* /*www  .ja v a 2  s.  co m*/
 * Licensed 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 java.awt.Color;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractButton;

import javax.swing.JToggleButton;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class Main {
    /**
     * Set the button to have simplified UI.
     * 
     * @param button
     *            button to be modified
     * @param <T>
     *            type of button
     * @return button
     */
    public static <T extends AbstractButton> T decoratedToSimpleButton(final T button) {

        button.setForeground(Color.BLACK);
        button.setBackground(Color.LIGHT_GRAY);
        button.setBorderPainted(true);
        button.setFocusPainted(true);
        button.setContentAreaFilled(false);
        button.setOpaque(true);

        if (button instanceof JToggleButton) {
            ((JToggleButton) button).addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (button.isSelected()) {
                        button.setBackground(Color.WHITE);
                    }
                }
            });
        }
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                super.mouseEntered(e);
                button.setBackground(Color.WHITE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                super.mouseExited(e);
                if (!button.isSelected()) {
                    button.setBackground(Color.LIGHT_GRAY);
                }
            }
        });

        button.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                if (!button.isSelected()) {
                    button.setBackground(Color.LIGHT_GRAY);
                }
            }
        });
        Border line = new LineBorder(Color.BLACK);
        Border margin = new EmptyBorder(5, 15, 5, 15);
        Border compound = new CompoundBorder(line, margin);
        button.setBorder(compound);
        return button;
    }
}

Related

  1. configureOKAndCancelButtons(JPanel panel, Action ok, Action cancel)
  2. configureTextAndMnemonic(AbstractButton button, String text)
  3. confirm(Component comp, String title, String message, int buttons, int type)
  4. creaStyledButton(int style)
  5. customize(AbstractButton btn)
  6. doClick(final AbstractButton button)
  7. doHover(boolean b, AbstractButton... btns)
  8. doSetText(final AbstractButton abstractButton, final String text)
  9. doSetTextInEDT(final AbstractButton abstractButton, final String text)