new JButton(Action act) : JButton « javax.swing « Java by API






new JButton(Action act)

  
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;

public class MainClass {

  public static void main(String args[]) {
    JFrame frame = new JFrame("Action Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final Action printAction = new PrintHelloAction();

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);
    menu.add(new JMenuItem(printAction));

    JToolBar toolbar = new JToolBar();
    toolbar.add(new JButton(printAction));

    JButton enableButton = new JButton("Enable");
    ActionListener enableActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        printAction.setEnabled(true);
      }
    };
    enableButton.addActionListener(enableActionListener);

    JButton disableButton = new JButton("Disable");
    ActionListener disableActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        printAction.setEnabled(false);
      }
    };
    disableButton.addActionListener(disableActionListener);

    JButton relabelButton = new JButton("Relabel");
    ActionListener relabelActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        printAction.putValue(Action.NAME, "Hello, World");
      }
    };
    relabelButton.addActionListener(relabelActionListener);

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(enableButton);
    buttonPanel.add(disableButton);
    buttonPanel.add(relabelButton);

    frame.setJMenuBar(menuBar);

    frame.add(toolbar, BorderLayout.SOUTH);
    frame.add(buttonPanel, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);

  }

}

class PrintHelloAction extends AbstractAction {

  PrintHelloAction() {
    super("Print");
    putValue(Action.SHORT_DESCRIPTION, "Hello, World");
  }

  public void actionPerformed(ActionEvent actionEvent) {
    System.out.println("Hello, World");
  }
}

           
         
    
  








Related examples in the same category

1.Button.background
2.Button.font
3.Button.foreground
4.new JButton(Icon ic)
5.new JButton(String text)
6.new JButton(String str) (HTML String)
7.new JButton(String text, Icon icon)
8.JButton: addActionListener(ActionListener act)
9.JButton: addAncestorListener(AncestorListener listener)
10.JButton: addChangeListener(ChangeListener l)
11.JButton: addItemListener(ItemListener l)
12.JButton: addPropertyChangeListener(PropertyChangeListener lis)
13.JButton: contains(int x, int y)
14.JButton: createToolTip()
15.JButton: getAccessibleContext()
16.JButton: getActionMap()
17.JButton: getBackground()
18.JButton: getInputMap()
19.JButton: removeActionListener(ActionListener l)
20.JButton: setActionCommand(String text)
21.JButton: setAlignmentX(float alignmentX)
22.JButton: setAlignmentY(float alignmentY)
23.JButton: setBackground(Color c)
24.JButton: setBorder(Border border)
25.JButton: setBorderPainted(boolean b)
26.JButton: setDefaultCapable(boolean defaultCapable)
27.JButton: setDisabledIcon(Icon disabledIcon)
28.JButton: setDisabledSelectedIcon(Icon disabledSelectedIcon)
29.JButton: setHorizontalAlignment(int alignment)
30.JButton: setIconTextGap(int iconTextGap)
31.JButton: setMnemonic(KeyEvent.VK_A)
32.JButton: setPressedIcon(Icon pressedIcon)
33.JButton: setRolloverIcon(Icon rolloverIcon)
34.JButton: setRolloverSelectedIcon(Icon rolloverSelectedIcon)
35.JButton: setSelectedIcon(Icon selectedIcon)
36.JButton: setToolTipText(String str)
37.JButton: setVerticalAlignment(int alignment)
38.extends JButton