0

When I press the JButton button1, I need the dropPoison method to be called in another class. How do I do this?

Main Class

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

public class Main extends JFrame implements ActionListener {
  private boolean PoisonTrue;
  private Player player1;
  private Player activePlayer;
  private boolean player1Poison;

  public Main() // creating the Frame
  {
    // code
  }

  public void actionPerformed(ActionEvent evt) {
    if (evt.getSource() instanceof TokenButton) {

      TokenButton button1 = (TokenButton) evt.getSource();

      if (PoisonTrue == true) {
        if (this.activePlayer == player1) {
          // Call dropPoison here
          player1Poison = true;
          PoisonTrue = false;
        }
      }
    }
  }
}

Token Button Class

import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JButton;

public class TokenButton extends JButton {

  ArrayList<Cell> cells;

  int nxtToken;

  TokenButton[] buttons;

  Grid gridPanel;

  public TokenButton() {
    nxtToken = 19;
    cells = new ArrayList<Cell>();
  }

  public int dropToken(Player player) {
    if (this.nxtToken != -1) {
      // code
    }

    return nxtToken;
  }

  public void dropPoison(Cell selectedCell, Grid grid) {
    int x = 0;
    int y = 0;
    this.gridPanel = grid;

    int xAxis = selectedCell.getXPosition();
    int yAxis = selectedCell.getYPosition();

    // North
    if (yAxis > 0) {
      grid.gridCells[x][y - 1].setBackground(Color.BLACK);
      grid.gridCells[x][y].setBackground(Color.GREEN);
    }
  }
}

I need the dropPoison method to be called when in the Main class I use button1 event.

4
  • 2
    1) Why do you have a class extend JButton especially since you're not changing any of JButton's innate properties or behaviors? 2) Why not simply use JButtons? 3) Why does the TokenButton class contain an array of TokenButtons? If you initialized those arrays, you would have a stackoverflow error for sure. Consider redesigning your program from first principles. Jul 15, 2015 at 21:09
  • 1
    Also as a general rule, you should not have your GUI classes implement your listener interfaces. While this is OK for extremely small "toy" programs, it does not work well with larger multiclass complex programs and leads to increased code complexity and connections (cyclomatic complexity) with creation of large classes that are difficult to test and debug. Jul 15, 2015 at 21:19
  • 2
    And as a more general rule: please do not post such ill-formated code.
    – Turing85
    Jul 15, 2015 at 21:20
  • @durron597 Yes. This is, what I am referring to ;)
    – Turing85
    Jul 15, 2015 at 21:43

1 Answer 1

3

So, instead of testing for Buttons with instanceof, do something like this.

  • Create a button in your JFrame.
  • Register an Action Listener with the instance of the JButton.
  • Do your work in actionPerformed (i.e. call dropPoison)
  • Add the JButton to the JFrame.

You are breaking a lot of OO conventions writing it this way. Build off the example below:

public class Parent {
    private JFrame myFrame;
    public Parent() {
        myFrame = new JFrame();
        JButton button = new JButton();
        button.addActionListener(new ActionListener() {
            @Override 
            public void actionPerformed(ActionEvent theEvent) {
                call drop poison here or whatever method you need to call
            }
        });
        frame.add(button, BorderLayout.CENTER);  
    }

    public static void main(String[] args) {
        new Parent();
    }
}
2
  • 1
    I'm impressed with your ability to answer this question before I even was able to get the code to be legibile. Well done
    – durron597
    Jul 15, 2015 at 21:18
  • Minor detail: you construct the JFrame and the JButton, but you never actually place the JButton on the JFrame.
    – Turing85
    Jul 15, 2015 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.