Example usage for javax.swing JButton setIcon

List of usage examples for javax.swing JButton setIcon

Introduction

In this page you can find the example usage for javax.swing JButton setIcon.

Prototype

@BeanProperty(visualUpdate = true, description = "The button's default icon")
public void setIcon(Icon defaultIcon) 

Source Link

Document

Sets the button's default icon.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JButton button = new JButton();
    // Add or change the icon; it will appear to the left of the text
    button.setIcon(new ImageIcon("icon.gif"));

    // Set to null to remove icon
    button.setIcon(null);/*w  w w .  j av a2 s.  c o  m*/
}

From source file:Main.java

public static void main(final String args[]) {
    JButton button = new JButton("Test");

    button.setHorizontalTextPosition(SwingConstants.LEFT);
    button.setIcon(new ImageIcon("r.gif"));
    button.setRolloverIcon(new ImageIcon("b.gif"));
    button.setRolloverEnabled(true);// w ww.j a v  a  2s  . co  m

    JOptionPane.showMessageDialog(null, button);
}

From source file:CustomTreeCellRenderer.java

public static void main(String[] args) {
    ImageIcon iconWhite = new ImageIcon("white.jpg");
    ImageIcon iconBlack = new ImageIcon("black.jpg");
    ;/* ww  w . j  a v a  2 s.c  o m*/
    JFrame frame = new JFrame();
    frame.setContentPane(new JPanel(new BorderLayout()));

    JTree tree = new JTree();
    frame.getContentPane().add(tree);

    CustomTreeCellRenderer renderer = new CustomTreeCellRenderer();
    renderer.setRendererIcon(iconWhite);
    tree.setCellRenderer(renderer);

    JPanel panelButtons = new JPanel();

    JButton buttonWhite = new JButton("");
    buttonWhite.setIcon(iconWhite);
    JButton buttonBlack = new JButton("");
    buttonBlack.setIcon(iconBlack);

    buttonBlack.addActionListener(e -> {
        renderer.setRendererIcon(iconBlack);
        tree.repaint();
    });

    panelButtons.add(buttonBlack);
    panelButtons.add(buttonWhite);
    frame.getContentPane().add(panelButtons, BorderLayout.SOUTH);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Convenience method for creating buttons with an icon and no text.  Wraps
 * the button in an empty border to give it some space.
 * @param icon the icon to display/*from   w  w w .  j  av a2s.  c  o  m*/
 * @param action the action to perform on click
 * @return a new JButton with the given icon
 */
public static JButton iconButton(Icon icon, Action action) {
    final JButton result = new JButton(action);
    result.setIcon(icon);
    result.setBorder(new EmptyBorder(4, 4, 4, 4));
    return result;
}

From source file:Main.java

/**
 * Determine the maximum size for a 2-state button with the specified text and icons.
 * This can be used to make sure that a button doesn't resize during state change.
 *
 * @param button  the UI of this JButton is used for size determination
 * @param string1 text for 1st mode/* w  ww  .  ja v  a 2  s  .c o  m*/
 * @param icon1   icon for 1st mode
 * @param string2 text for 2nd mode
 * @param icon2   icon for 2nd mode
 * @return the Dimension that contains both modes for the button.
 */
public static Dimension getMaxDimension(JButton button, String string1, ImageIcon icon1, String string2,
        ImageIcon icon2) {

    String originalText = button.getText();
    Icon originalIcon = button.getIcon();

    // Get dimensions for "Play" state
    button.setText(string1);
    button.setIcon(icon1);
    Dimension playSize = button.getUI().getPreferredSize(button);

    // Get dimensions for "Pause" state
    button.setText(string2);
    button.setIcon(icon2);
    Dimension pauseSize = button.getUI().getPreferredSize(button);

    // Restore original text and icon
    button.setText(originalText);
    button.setIcon(originalIcon);

    // Return max dimensions
    int maxWidth = (int) Math.max(playSize.getWidth(), pauseSize.getWidth());
    int maxHeight = (int) Math.max(playSize.getHeight(), pauseSize.getHeight());
    return new Dimension(maxWidth, maxHeight);
}

From source file:eu.europa.ec.markt.dss.applet.util.ComponentFactory.java

/**
 * //from w w  w  .ja  va 2 s.co m
 * @param label
 * @param enabled
 * @param actionListener
 * @param icon
 * @return
 */
public static JButton createButton(final String label, final boolean enabled,
        final ActionListener actionListener, final Icon icon) {
    final JButton button = createButton(label, enabled, actionListener);
    button.setIcon(icon);
    return button;
}

From source file:com.diversityarrays.kdxplore.vistool.VisToolbarFactory.java

static public VisToolToolBar create(final String title, final JComponent comp, final Closure<File> snapshotter,
        final VisToolDataProvider visToolDataProvider, boolean floatable, final String[] imageSuffixes) {
    Window window = GuiUtil.getOwnerWindow(comp);

    boolean anyButtons = false;

    final JCheckBox keepOnTop;

    if (window == null) {
        keepOnTop = null;/*from  ww  w  .  java2s.  c  o  m*/
    } else {
        anyButtons = true;
        keepOnTop = new JCheckBox(Msg.OPTION_KEEP_ON_TOP(), true);

        keepOnTop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                window.setAlwaysOnTop(keepOnTop.isSelected());
            }
        });
        window.setAlwaysOnTop(keepOnTop.isSelected());

        //         buttons.add(keepOnTop);

        final PropertyChangeListener alwaysOnTopListener = new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                keepOnTop.setSelected(window.isAlwaysOnTop());
            }
        };
        window.addPropertyChangeListener(PROPERTY_ALWAYS_ON_TOP, alwaysOnTopListener);

        window.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                window.removeWindowListener(this);
                window.removePropertyChangeListener(PROPERTY_ALWAYS_ON_TOP, alwaysOnTopListener);
            }
        });
    }

    final JButton cameraButton;
    if (snapshotter == null) {
        cameraButton = null;
    } else {
        Action cameraAction = new AbstractAction(Msg.ACTION_SNAPSHOT()) {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (chooser == null) {
                    chooser = new JFileChooser();
                    chooser.setFileFilter(new FileFilter() {
                        @Override
                        public boolean accept(File f) {
                            if (!f.isFile()) {
                                return true;
                            }
                            String loname = f.getName().toLowerCase();
                            for (String sfx : imageSuffixes) {
                                if (loname.endsWith(sfx)) {
                                    return true;
                                }
                            }
                            return false;
                        }

                        @Override
                        public String getDescription() {
                            return Msg.DESC_IMAGE_FILE();
                        }
                    });
                    chooser.setMultiSelectionEnabled(false);
                    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                }

                if (JFileChooser.APPROVE_OPTION == chooser.showSaveDialog(comp)) {
                    File file = chooser.getSelectedFile();
                    snapshotter.execute(file);
                }
            }
        };

        ImageIcon icon = loadIcon("camera-24.png"); //$NON-NLS-1$
        if (icon != null) {
            cameraAction.putValue(Action.SMALL_ICON, icon);
            cameraAction.putValue(Action.NAME, null);
        }

        anyButtons = true;
        cameraButton = new JButton(cameraAction);
    }

    final JButton refreshButton;
    if (visToolDataProvider == null) {
        refreshButton = null;
    } else {
        anyButtons = true;

        refreshButton = new JButton(Msg.ACTION_REFRESH());

        ImageIcon icon = loadIcon("refresh-24.png"); //$NON-NLS-1$
        if (icon != null) {
            refreshButton.setIcon(icon);
            // don't remove the name
        }

        refreshButton.setForeground(Color.RED);
        refreshButton.setEnabled(false);

        refreshButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (visToolDataProvider.refreshData()) {
                    refreshButton.setEnabled(false);
                }
            }
        });

        visToolDataProvider.addVisToolDataChangedListener(new VisToolDataChangedListener() {
            @Override
            public void visToolDataChanged(Object source) {
                refreshButton.setEnabled(true);
            }
        });
    }

    VisToolToolBar toolBar = null;

    if (anyButtons) {
        toolBar = new VisToolToolBar(keepOnTop, cameraButton, refreshButton);
        toolBar.setFloatable(floatable);
    }
    return toolBar;

}

From source file:Main.java

/**
 * Returns a styled JButton./*  w ww  . j  av a2  s . c  o  m*/
 *
 * @param style button type
 * @return a styled button
 */
public static JButton creaStyledButton(int style) {
    JButton jb = new JButton();
    jb.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    jb.setMargin(new Insets(0, 5, 1, 5));
    switch (style) {
    case STYLE_EDIT: {
        jb.setText("edit");
        jb.setToolTipText("edit");
        jb.setPreferredSize(new Dimension(60, 30));
        return jb;
    }
    case STYLE_OK: {
        jb.setText("ok");
        jb.setToolTipText("confirm changes");
        jb.setPreferredSize(new Dimension(60, 30));
        return jb;
    }
    case STYLE_CANCEL: {
        jb.setText("cancel");
        jb.setToolTipText("discard changes");
        jb.setPreferredSize(new Dimension(60, 30));
        return jb;
    }
    case STYLE_INSBEFOREROW: {
        jb.setIcon(ICON_INSBEFOREROW);
        jb.setToolTipText("insert row before");
        jb.setName("INSBEFOREROW");
        return jb;
    }
    case STYLE_INSAFTERROW: {
        jb.setIcon(ICON_INSAFTERROW);
        jb.setToolTipText("insert row after");
        jb.setName("INSAFTERROW");
        return jb;
    }
    case STYLE_DELETEROW: {
        jb.setIcon(ICON_DELETEROW);
        jb.setToolTipText("delete row");
        jb.setName("DELETEROW");
        return jb;
    }
    case STYLE_CLONEBEFOREROW: {
        jb.setIcon(ICON_CLONEBEFOREROW);
        jb.setToolTipText("clone row before");
        jb.setName("CLONEBEFOREROW");
        return jb;
    }
    case STYLE_CLONEAFTERROW: {
        jb.setIcon(ICON_CLONEAFTERROW);
        jb.setToolTipText("clone row after");
        jb.setName("CLONEAFTERROW");
        return jb;
    }
    case STYLE_DEFAULTROWS: {
        jb.setIcon(ICON_DEFAULTROWS);
        jb.setToolTipText("set default values");
        jb.setName("DEFAULTROWS");
        return jb;
    }
    default: {
        return null;
    }
    }
}

From source file:Main.java

@Override
protected JButton createArrowButton() {
    JButton btn = new JButton();
    btn.setIcon(infoIcon);
    btn.setRolloverIcon(warnIcon);/*from ww w  . j a  va 2s.com*/
    return btn;
}

From source file:SampleButton.java

public SampleButton() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel(new BorderLayout());
    p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    JButton b = new JButton("Test");
    b.setHorizontalTextPosition(SwingConstants.LEFT);
    b.setIcon(new ImageIcon("r.gif"));
    b.setRolloverIcon(new ImageIcon("b.gif"));
    b.setRolloverEnabled(true);//from w w  w. j  a v  a 2  s.c om

    b.setMnemonic('t');
    b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Button pressed");
        }
    });
    p.add(b);
    getContentPane().add(p);
    pack();
}