user455318
user455318

Reputation: 3346

action listener in another class - java

it is possible to have two class, and in one something like

arrayButtons[i][j].addActionListener(actionListner);

and in another

ActionListener actionListner = new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            for (int j = 0; j < arrayButtons.length; j++) {
                for (int i = 0; i < arrayButtons[j].length; i++) {
                    if (arrayButtons[j][i] == e.getSource()) {

                        if ((gameNumber == 2) && (playHand.getNumberOfCards() == 0)) {
                            if (player[j].getCard(i).getSuit() == Suit.HEARTS.toString() && player[j].hasSuitBesideHearts())
                                //second game
                                messageOnTable("xxx");

                            else{
                                arrayButtons[j][i].setVisible(false);
                                test[j].setIcon(player[j].getCard(i).getImage());
                                pnCardNumber[j].setText(Integer.toString(player[j].getCard(i).getNumber()));
                                pnCardName[j].setText(player[j].getCard(i).toString());
                                pnCardSuit[j].setText(player[j].getCard(i).getSuit());

                                playHand.addCard(player[j].getCard(i), j);

                                player[j].removeCard(i);

                            }

                        }

}

//and more the reason of that is because i need to separate the button (swing) to the action listener

how i can do ?

thanks

Upvotes: 11

Views: 71226

Answers (5)

Goran Jovic
Goran Jovic

Reputation: 9508

Not only it is possible to separate these two, it's also recommended (see MVC pattern - it's very much about separating screen controls like buttons, and the logics of your program)

The easiest way that comes to my mind is to do write a named class that implements ActionListener interface, something like this:

public class SomeActionListener implements ActionListener{

    private JTextField textField1;
    private JComboBox combo1;
    private JTextField textField2;
    //...

    public SomeActionListener(JTextField textField1, JComboBox combo1, 
                                          JTextField textField2){
        this.textField1=textField1;
        this.combo1=combo1;
        this.textField2=textField2;
        //...
    }

    public void actionPerformed(ActionEvent e) {
        //cmd
    }

}

And then add it to your buttons:

ActionListener actionListener = new SomeActionListener(textField1, combo1, textField2);
someButton.addActionListener(actionListener);

Upvotes: 17

Fareed Alnamrouti
Fareed Alnamrouti

Reputation: 32154

you can do it easily by using nested classes, but i think the best way is pass the parent object as a parameter to the construct of object and using it as an action handler;

//**parent class - Calculator **//

public class Calculator extends JFrame implements ActionListener{

    private DPanel dPanel;
    private JTextField resultText;

    public Calculator(){
        // set calc layout
        this.setLayout(new BorderLayout(1,1));
        dPanel = new DPanel(this); // here is the trick ;)
    }
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        resultText.setText(command);
        // **** your code ****/
    }    
}



//**inner class - DPanel**//

public class DPanel extends JPanel{

    private JButton digitsButton[];
    private JButton dotButton,eqButton;

    public DPanel(Calculator parent){
        //layout
        this.setLayout(new GridLayout(4,3,1,1));

        // digits buttons
        digitsButton = new JButton[10];
        for (int i=9;i>=0;i--){
            digitsButton[i] = new JButton(i+"");
            digitsButton[i].addActionListener(parent); // using parent as action handler ;)
            this.add(digitsButton[i]);
        }
     }
}

Upvotes: 3

Oskar Lund
Oskar Lund

Reputation: 369

It's a bit off topic but you should definately not use the == operator to compare Strings as you appear to be doing on this line:

if (player[j].getCard(i).getSuit() == Suit.HEARTS.toString()

This is because Strings are pointers, not actual values, and you may get unexpected behaviour using the == operator. Use the someString.equals(otherString) method instead. And also

"String to compare".equals(stringVariable)

is alot better than the other way around

stringVariable.equals("String to compare to")

because in the first example you avoid getting a NullPointerException if stringVariable is null. It just returns false.

Upvotes: 2

Alejandro
Alejandro

Reputation: 289

Yes, it can be done. It's very simple; in one class you have your buttons, in the other class you just need to implement an ActionListener and just make your //cmd to separate that button's function. To do this, you need to use e.getActionCommand().equals(buttonActionCommand). Sample code:

public class Click implements ActionListener{

    public Click(
     //input params if needed
     ){

    }

     public void actionPerformed(ActionEvent e) {
     if( e.getActionCommand().equals(buttonActionCommand){
     //cmd
     }
     } 

}

To add that listener on your button just do:

buttonTest.addActionListener(new Click());

Upvotes: 1

Matt Wonlaw
Matt Wonlaw

Reputation: 12443

To answer: "my problem is that action listener have many variables of swing like buttons for example,so, when i change to another class, i have problems with that"

Your action listener class could have a constructor that takes a parameter of the type of the view class:

public class Listener implements ActionListener {
  private final MyViewClass mView;
  public Listener(MyViewClass pView) {
    mView = pView;
  }

  public void actionPerformed(ActionEvent e) {
    // can use mView to get access to your components.
    mView.get...().doStuff...
  }
}

Then in your view:

Listener l = new Listener(this);
button.addActionListener(l);

Upvotes: 6

Related Questions