make Swing Navigation Button - Java Swing

Java examples for Swing:JButton

Description

make Swing Navigation Button

Demo Code


import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class Main{
    public static JButton makeNavigationButton(String imgLocation,
            String actionCommand, String toolTipText, String altText,
            ActionListener listener) {
        // Look for the image.

        // Create and initialize the button.
        JButton button = new JButton();
        button.setActionCommand(actionCommand);
        button.setToolTipText(toolTipText);
        button.addActionListener(listener);
        if (altText != null) {
            button.setText(altText);/*from   ww w .ja  va2 s .c  o m*/
        }
        if (imgLocation != null) {
            // button.setIconTextGap(10);
            URL imageURL = ButtonUtil.class.getClassLoader().getResource(
                    imgLocation);
            if (imageURL != null) {
                button.setIcon(new ImageIcon(imageURL, altText));
                button.setSize(new Dimension(1, 1));

                // setOpaque(false);// image found
                button.setBorderPainted(false);
                // button.setContentAreaFilled(false);
                // button.setRolloverEnabled(true);
                button.setVerticalTextPosition(JButton.BOTTOM);
                button.setHorizontalTextPosition(JButton.CENTER);
                button.setIconTextGap(0);

            } else { // no image found
                System.err.println("Resource not found: " + imgLocation);
            }

        }

        return button;
    }
}

Related Tutorials