1

I have here just a snip of code for my button:

    up = new JButton(new ImageIcon("more_buttons\\up3.png"));
    up.setBackground(new Color(224,223,227));
    up.setPreferredSize(new Dimension(5,15));
    up.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        value1000++;    

        if(value1000>0)
        {
            number.setText(value1000+"");
            down.setEnabled(true);
        }
        }
    });


    down = new JButton(new ImageIcon("more_buttons\\down3.png"));
    down.setBackground(new Color(224,223,227));
    down.setPreferredSize(new Dimension(5,15));
    down.setEnabled(false);
    down.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            value1000--;

        if(value1000>0)
        {
            number.setText(value1000+"");
        }
        if(value1000==0)
        {
            number.setText(value1000+"");
            down.setEnabled(false);     
        }

        }
    });

I'm wondering if I can make an action command for this button so that I won't have to repeat this code throughout my program. I only have to call the function like buttonaction(e) or something like that. I'm not used to creating action command but I have used it before but only for appending text. I'm not sure how to do that with a function like this. Is it possible? Or is there a more efficient way to do this?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user3771102
  • 558
  • 2
  • 8
  • 27

3 Answers3

4

You can add the same ActionListener to multiple buttons:

ActionListener al = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // You can check which button was pressed and act accordingly
        // simply by checking the event source:
        if (e.getSource() == button1)
            System.out.println("Button1 was pressed.");
        else if (e.getSource() == button2)
            System.out.println("Button2 was pressed.");
    }
};

button1.addActionListener(al);
button2.addActionListener(al);
icza
  • 389,944
  • 63
  • 907
  • 827
4

To remove boiler plate code, You need to at least implement an ActionListener in your class

samaple:

public class myClass implements ActionListener

It will generate an actionPerformed method After you need to add actionCommand in your button so when you click a button it will recognize it that you pressed that button

sample:

down.setActionCommand("down");
down.addActionListener(this);
up.setActionCommand("up");
up.addActionListener(this);

in the actionPerformed method

@Override
    public void actionPerformed(ActionEvent evt) 
    {
        String actionCommand = evt.getActionCommand(); //get the actionCommand and pass it to String actionCommand


        switch(actionCommand) { //switch statement for each of the action command 
            case "down": 
                //down button command here
                break;
            case "up": 
                //up button command here
            }
    }
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
3

Take a look at How to use Actions

public abstract class AbstractNumberValueAction extends AbstractAction {
    private NumberModel model;
    private JTextField numberField;
    private int delta;

    public ValueAction(NumberModel model, JTextField numberField, int delta) {
        this.model = model;
        this.numberField = numberField;
        this.delta = delta;
    }

    public void actionPerformed(ActionEvent evt) {

        int value1000 = model.updateValue(delta);

        if(value1000>0)
        {
            numberField.setText(value1000+"");
        }
        if(value1000==0)
        {
            numberField.setText(value1000+"");
            setEnabled(false);     
        }
    }
}

public class UpAction extends AbstractNumberValueAction {

    public ValueAction(NumberModel model, JTextField numberField) {
        this(model, numberField, 1);
        putValue(SMALL_ICON, new ImageIcon("more_buttons\\up3.png"));
    }
}

public class DownAction extends AbstractNumberValueAction {

    public ValueAction(NumberModel model, JTextField numberField) {
        this(model, numberField, -1);
        putValue(SMALL_ICON, new ImageIcon("more_buttons\\down3.png"));
    }
}

Which could then simply be applied as

up = new JButton(new UpAction(model, number));
down = new JButton(new DownAction(model, number));

For example...

(ps- NumberModel would be a simple class that controlled the underlying value to make is simpler to manage ;))

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366