Class Button

All Implemented Interfaces:
ActionListener, ImageObserver, ItemSelectable, MenuContainer, PropertyChangeListener, Serializable, Cloneable, EventListener, SwingConstants

public class Button extends AbstractButton implements ActionListener, Cloneable, PropertyChangeListener

A Button object is a button that can take different configuration. It can be sticky or not, it can popup a menu defined by an array of ButtonDef, etc.

If a group is specified :
Initially, all buttons in the group are unselected. Once any button is selected, one button is always selected in the group. There is no way to turn a button programmatically to "off", in order to clear the button group.
Examples of configurations

  • Normal button (w/o text) :

      Button(new ButtonDef("theAction", anIcon, null, true), false, new Dimension(32, 32));


  • Normal sticky button (with text) :

      Button(new ButtonDef("theAction", anIcon, "Example", true), true, new Dimension(32, 32));


  • Normal sticky button with a group :

      Button(new ButtonDef("theAction", anIcon, "Example", true), aButtonGroup, new Dimension(32, 32));


  • A button (w/o little pop bar) that will popup a menu. The user can select an item in the menu. The button will not be updated with the selection made in the menu. The dimension will be calculated.

     Define some ButtonDef :

      ButtonDef bDefA = new ButtonDef("actionA", anIcon, "Choice A", false);
      ButtonDef bDefB = new ButtonDef("actionB", anIcon, "Choice B", false);
      ButtonDef bDefC = new ButtonDef("actionC", anIcon, "Choice C", false);

     Create the button :

      Button(anIcon, "List of actions", false, new ButtonDef[]{bDefA, bDefB, bDefC}, null);


  • A button that will popup a menu when the user clicks on the little pop bar. If the user clicks directly on the button, the "Action B" (idx 1) will be fired. The default icon / text displayed is "Action A" (idx 0).

     Define some ButtonDef :

      ButtonDef bDefA = new ButtonDef("actionA", anIcon, "Choice A", false);
      ButtonDef bDefB = new ButtonDef("actionB", anIcon, "Choice B", false);
      ButtonDef bDefC = new ButtonDef("actionC", anIcon, "Choice C", false);

     Create the button :

      Button(new ButtonDef[]{bDefA, bDefB, bDefC}, 0, 1, new Dimension(44, 32));


  • A button that will popup a menu when the user clicks on the little pop bar. The button will be updated with the choice made by the user. If the user clicks directly on the button, the current action will be fired. The button is not sticky in this example. The default definition displayed is "Action A" (idx 0).

     Define some ButtonDef :

      ButtonDef bDefA = new ButtonDef("actionA", anIcon, "Choice A", false);
      ButtonDef bDefB = new ButtonDef("actionB", anIcon, "Choice B", false);
      ButtonDef bDefC = new ButtonDef("actionC", anIcon, "Choice C", false);

     Create the button :

      Button(new ButtonDef[]{bDefA, bDefB, bDefC}, 0, false, new Dimension(44, 32));


  • A button that will popup a menu when the user clicks on the little pop bar. The button will be updated with the choice made by the user. If the user clicks directly on the button, the current action will be fired. The button is sticky in this example and is appart of a button group. The default definition displayed is "Action C" (idx 2).

     Define some ButtonDef :

      ButtonDef bDefA = new ButtonDef("actionA", anIcon, "Choice A", false);
      ButtonDef bDefB = new ButtonDef("actionB", anIcon, "Choice B", false);
      ButtonDef bDefC = new ButtonDef("actionC", anIcon, "Choice C", false);

     Create the button :

      Button(new ButtonDef[]{bDefA, bDefB, bDefC}, 2, true, aButtonGroup, new Dimension(44, 32));
See Also:
  • Constructor Details

    • Button

      public Button()
      Default constructor. For java bean compatibility.
    • Button

      public Button(ButtonDef buttonDef, boolean sticky, Dimension dimension)
      Construct a normal button. Sticky option can be specified.
      Parameters:
      buttonDef - the definition of this button
      sticky - the sticky state
      dimension - the dimension of the button. If null, the dimension will be calculated depending on the icon and/or the text
    • Button

      public Button(Action action, boolean sticky, Dimension dimension)
    • Button

      public Button(ButtonDef buttonDef, ButtonGroup buttonGroup, Dimension dimension)
      Constructs a normal sticky button with a button group.
      Parameters:
      buttonDef - the definition of this button
      buttonGroup - a button group. The button group can't be null.
      dimension - the dimension of the button. If null, the dimension will be calculated depending on the icon and/ord the text
    • Button

      public Button(Action action, ButtonGroup buttonGroup, Dimension dimension)
    • Button

      @Deprecated public Button(ImageIcon icon, String text, boolean centerIconText, ButtonDef[] buttonDefs, Dimension dimension)
      Deprecated.
      use Button(ImageIcon, String, boolean, ButtonDef[], false, Dimension)
      Constructs a button that will pop a popup menu when the user click on it. There is no little pop bar with this constructor and no default action. The button will not be updated with the choice made by the user.
      Parameters:
      icon - the icon to be displayed. Can be null
      text - the text to be displayed. Can be null
      centerIconText - center state of the icon/text
      buttonDefs - an array of button definitions
      dimension - the dimension of the button. If null, the dimension will be calculated depending on the icon and/ord the text
    • Button

      public Button(ImageIcon icon, String text, boolean centerIconText, ButtonDef[] buttonDefs, boolean updateButtonWithUserChoice, Dimension dimension)
      Constructs a button that will pop a popup menu when the user click on it. There is no little pop bar with this constructor and no default action.
      Parameters:
      icon - the icon to be displayed. Can be null
      text - the text to be displayed. Can be null
      centerIconText - center state of the icon/text
      buttonDefs - an array of button definitions
      updateButtonWithUserChoice - if true, the button will be updated with the user's choice
      dimension - the dimension of the button. If null, the dimension will be calculated depending on the icon and/ord the text
    • Button

      public Button(ButtonDef[] buttonDefs, int defaultActionIdx, int popupDefaultActionIdx, Dimension dimension)
      Constructs a button that displays a popup menu when the user clicks on the little pop bar. The button will not be updated with the choice made by the user. If the user clicks directly on the button, the popupDefaultActionIdx will be fired. This button will not be sticky.
      Parameters:
      buttonDefs - an array of button definitions
      defaultActionIdx - the default button definition displayed
      popupDefaultActionIdx - the action name to be fired when the user clicks directly on the button
      dimension - the dimension of the button. If null, the dimension will be calculated depending on the icon and/or the text
    • Button

      public Button(ButtonDef[] buttonDefs, int defaultActionIdx, boolean sticky, Dimension dimension)
      Constructs a button that will be updated with the choice made in the popup menu. The popup menu will be displayed if the user click on the little pop bar. The current action will be fired if the user clicks on the button. The initial button definition displayed is specified by the defaultActionIdx. Sticky option can be specified.
      Parameters:
      buttonDefs - an array of button definitions
      defaultActionIdx - the default button definition displayed
      sticky - sticky option
      dimension - the dimension of the button. If null, the dimension will be calculated depending on the icon and/or the text
    • Button

      public Button(ButtonDef[] buttonDefs, int defaultActionIdx, boolean sticky, ButtonGroup buttonGroup, Dimension dimension)
      Constructs a button that will be updated with the choice made in the popup menu. The popup menu will be displayed if the user clicks on the little pop bar. The initial button definition displayed is specified by the defaultActionIdx. Sticky option can be specified but if the button group specified is not null, sticky option will be set to true.
      Parameters:
      buttonDefs - an array of button definitions
      defaultActionIdx - the default button definition displayed
      sticky - sticky option. (Overriden if the buttonGroup is not null)
      buttonGroup - a button group. If not null, the sticky option is sets to true
      dimension - the dimension of the button. If null, the dimension will be calculated depending on the icon and/or the text
  • Method Details

    • setFixedHeight

      public void setFixedHeight(int height)
      Sets the height of the button.
    • setFixedWidth

      public void setFixedWidth(int width)
      Sets the width of the button.
    • setFixedSize

      public void setFixedSize(Dimension dimension)
      Sets the size of the button. Note: a null size has no effects
      Parameters:
      dimension - new size
    • setIcon

      public void setIcon(int colorRGB, int w, int h)
      Creates and sets an icon of a specific RGB color. Note: A black border will be added
    • setIcon

      public void setIcon(Image imgTexture, int w, int h)
      Sets the icon of the current button definition. Note: A black border will be added
      Parameters:
      imgTexture - the icon
      w - the width
      h - the height
    • setIcon

      public void setIcon(Icon icon)
      Sets the icon of the current button definition.
      Overrides:
      setIcon in class AbstractButton
      Parameters:
      icon - the new icon
    • setText

      public void setText(int buttonDefIdx, String newText)
      Change the text for a button definition.
      Parameters:
      buttonDefIdx - the idx to change
      newText - the new text
    • setText

      public void setText(String newText)
      Change the current visible text of the button. A null value remove the current text.
      Overrides:
      setText in class AbstractButton
      Parameters:
      newText - the text to display.
    • getText

      public String getText()
      Returns the current visible text.
      Overrides:
      getText in class AbstractButton
      Returns:
      text of the button; null if there is no text
    • isSelected

      public boolean isSelected()
      Returs the selected state of this button. Note: Only sticky buttons with a button group are affected by this method.
      Overrides:
      isSelected in class AbstractButton
      Returns:
      selected state
    • setSelected

      public void setSelected(boolean isSelected)
      Sets the selected state of the button. Note: Only sticky buttons with a button group are affected by this method.
      Overrides:
      setSelected in class AbstractButton
      Parameters:
      isSelected - selected state
    • getCurrentActionName

      public String getCurrentActionName()
      Returns the current action name of the button
      Returns:
      the action name
    • paintComponent

      public void paintComponent(Graphics g)
      Overrides:
      paintComponent in class JComponent
    • processMouseEvent

      protected void processMouseEvent(MouseEvent mouseevent)
      Overrides:
      processMouseEvent in class JComponent
    • actionPerformed

      public void actionPerformed(ActionEvent ae)
      Specified by:
      actionPerformed in interface ActionListener
    • setCurrentAction

      public void setCurrentAction(String actionName)
    • clone

      public Object clone()
      Clones the Button
      Overrides:
      clone in class Object
      Returns:
      a cloned Button
    • propertyChange

      public void propertyChange(PropertyChangeEvent pce)
      Specified by:
      propertyChange in interface PropertyChangeListener
    • setSize

      public void setSize(Dimension dim)
      Overrides:
      setSize in class Component
    • setSize

      public void setSize(int width, int height)
      Overrides:
      setSize in class Component
    • setSticky

      public void setSticky(boolean sticky)