3

For the longest time I have been invoking getActionCommand on ActionEvent objects to retrieve information from some JButtons, but as my programs grew more complex, I wanted to send several bits of information through setActionCommand, i.e. having a command like "r3" to indicate to the Action Listener that I wanted to remove the 3rd button from a panel within a JFrame. Eventually, I grew tired of parsing the strings and extracting the information that I wanted to use and instead started using getSource. (I want to know which one is better to use to retrieve information)

Also, I created a subclass of JButton called OperationButton that has two instance fields: an int ID and an Operation op (Operation is a custom enumerated type whose values are ADD, REMOVE, SWITCH, etc). I want to know if the following method is more efficient/a better practice than simply using getActionCommand, or if there is a third way of handling events that I have not thought of yet.

public void actionPerformed(ActionEvent e)
{
   OperationButton opButton = (OperationButton) e.getSource();
   int ID = opButton.getID();
   Operation op = opButton.getOperation();

   switch (op)
   {
    case ADD: //adds a custom panel to frame
    break;
    case REMOVE: //removes a button and a custom panel with the specified ID 
    break;
    case SWITCH: //highlights a button with the specified ID and 
                 //displays a custom panel with the specified ID
    //...
   }
}

(OperationButtons are the only buttons in my program)

Again, I want to retrieve information without having to set the action command of a JButton, but I'm not entirely sure if this is the right way to go. Also, would this method be feasible for future programs in which I might want to send more than 2 pieces of information?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
novice
  • 285
  • 1
  • 2
  • 9
  • 5
    Use an [`AbstractAction`](http://docs.oracle.com/javase/8/docs/api/javax/swing/AbstractAction.html) (or 17..) and the problem solves itself, in that the `Action` encapsulates the command as well as the action to take & the source (which might be both a menu and a button) becomes irrelevant (as does the string command itself). See [How to Use Actions](https://docs.oracle.com/javase/tutorial/uiswing/misc/action.html) for more details & working examples. – Andrew Thompson Aug 02 '15 at 01:52
  • So, would I create an AddAction and add it to the appropriate buttons and create similar actions for other kinds of buttons? Also, I know that with Actions you can use icons, set text, and a centralized ability to set components enabled/disabled, but what if I don't want to use those features? Wouldn't I be okay with creating classes that merely implement ActionListener? – novice Aug 02 '15 at 10:15
  • 1
    `AbstractAction` implements `ActionListener`, for [example](http://stackoverflow.com/a/4039359/230513); you can ignore unused features. – trashgod Aug 02 '15 at 15:17
  • 1
    *"So, would I create an AddAction and add it to the appropriate buttons and create similar actions for other kinds of buttons?"* Sounds like a plan.. *"Also, I know that with Actions you can use icons, set text, and a centralized ability to set components enabled/disabled, but what if I don't want to use those features?"* Then don't use them. There is nothing compulsory about them. *"Wouldn't I be okay with creating classes that merely implement ActionListener?"* For each button, or a common one for 'all other buttons and menu items? I would suggest you are not saving much code by .. – Andrew Thompson Aug 02 '15 at 15:18
  • .. creating an `ActionListener` rather than an `Action`. For the latter idea, it again opens the problem of distinguishing between action commands or event sources. The idea (here) is to encapsulate all the information needed for a button (and/or menu item) & what happens when it is activated by the user. See also the comment of @trashgod. – Andrew Thompson Aug 02 '15 at 15:21
  • But if an Action is an ActionListener, then isn't the only difference between them is the added functionality that I don't need? If I extend AbstractAction, then all I really need to do is implement actionPerformed like I would have done for a class implementing ActionListener anyway. I still don't understand how Actions are better at "encapsulating all the information needed for a button". I could still create an instance of a class implementing ActionListener and attach it to multiple buttons. – novice Aug 03 '15 at 19:42
  • Also, the reason I am asking the main question is because I have two custom JPanels in my program: one having a dynamic ArrayList of JButtons and the other having a dynamic ArrayList of MatrixPanel objects (MatrixPanel extends JPanel). MatrixPanel objects are arranged in a CardLayout. Clicking on a particular button in the first major JPanel shows the corresponding MatrixPanel object. You could say that the ArrayLists mentioned are parallel arrays. So, each button needs a unique ID. I'm stuck between subclassing JButton or doing something differently with Action. – novice Aug 04 '15 at 02:55
  • 1
    Tip: Add @trashgod (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Aug 04 '15 at 08:00

0 Answers0