kskaradzinski
kskaradzinski

Reputation: 5084

java call action from another action

countresultsfrom.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) 
                {
                    Color orginalColor = mcoef.getBackground();
                    switch(countresultsfrom.getSelectedIndex())
                    {
                        case 0: // Mech Cnt;
                            mtotal.setBackground(Color.YELLOW); 

                            if(mstatus.getSelectedIndex() == 2)
                            {
                                countresultsfrom.setSelectedIndex(2);
                                // countresultsfrom <----- CALL EVENT ???
                            }

                            etotal.setBackground(orginalColor);
                            ctotal.setBackground(orginalColor);
                        break;
                        case 1: // El Cnt;
                            etotal.setBackground(Color.YELLOW);

                            if(estatus.getSelectedIndex() == 2)
                            {
                                countresultsfrom.setSelectedIndex(2);
                            }

                            mtotal.setBackground(orginalColor);
                            ctotal.setBackground(orginalColor);
                        break;
                        case 2:
                            ctotal.setBackground(Color.YELLOW);

                            etotal.setBackground(orginalColor);
                            mtotal.setBackground(orginalColor);
                        break;
                    }
                }
            });

how to call listener one more time ???

Upvotes: 1

Views: 4687

Answers (4)

mKorbel
mKorbel

Reputation: 109823

your descriptions isn't clear for me, but there is basicall two ways

1/ create own Class someName implements ActionListener

2/ create java.swing.Action some examples for that on Action

EDIT:

if your countresultsfrom is JList, then all ours advices (maybe) didn't correct at all http://download.oracle.com/javase/tutorial/uiswing/components/list.html

Upvotes: 2

Santosh S
Santosh S

Reputation: 71

First of all extract your action performed method to a separate class in your code. It could be a private static class in the same class you wish to call. e.g

private static class ColorActionHandler implements ActionListener {
   //implement your method here by looping twice. or create a method for your logic and then call it in a for loop in the actionPerformed method
}

and pass an instance of this class in the addActionListener of your countresultsfrom button.

Upvotes: 2

MByD
MByD

Reputation: 137392

  1. You could simply call yourListener.actionPerformed(/*some event*/ e). Note that it will not be handled as an event, but as a regular method call.
  2. #1 is the simple technical way. It is not recommended. If you want some logic to occur, wrap it in a method and call that method, ActionListeners are meant for handling GUI events.

Upvotes: 2

Synesso
Synesso

Reputation: 38988

Do you wish actionPerformed(ActionEvent e) to be invoked twice on every action?

If so, wrap the block starting with Color orginalColor = mcoef.getBackground() in a for-loop. for (int i=0; i<2; i++) { ... }

Upvotes: 1

Related Questions