Java JButton Create createSimpleTextButton(String caption, Action action)

Here you can find the source of createSimpleTextButton(String caption, Action action)

Description

Create simple JButton .

License

Apache License

Parameter

Parameter Description
caption caption on the button
action action

Declaration

public static JButton createSimpleTextButton(String caption, Action action) 

Method Source Code


//package com.java2s;
/* /*from w  ww .j  a  va 2 s  .c  o 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.Action;
import javax.swing.JButton;

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 {
    /**
     * Create simple {@link JButton}.
     * 
     * @param caption
     *            caption on the button
     * @param action
     *            action
     * @return {@link JButton}
     */
    public static JButton createSimpleTextButton(String caption, Action action) {
        JButton button = decoratedToSimpleButton(new JButton(caption));
        button.addActionListener(action);
        return button;
    }

    /**
     * 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. createRadioButton(String name, String command, boolean isSelected, ActionListener listener)
  2. createRadioButton(String text, ButtonGroup buttonGroup, ActionListener... listeners)
  3. createRadioButton(String text, int mnemonic, ActionListener listener, boolean selected)
  4. createRadioButtonGrouping(String elements[], String title)
  5. createScreenCaptureButton()
  6. createSmallButton()
  7. createSmallButton(Icon icon, String tooltip, ActionListener listener)
  8. createSquareButton(String text, int size)
  9. createStandartButton(String name)