Win a copy of Penetration Testing Basics this week in the Security forum!
    Bookmark Topic Watch Topic
  • New Topic

actionListener help w/sample code

Justin Fenety
Posts: 3
  • Mark post as helpful
  • send pies
  • Report post to moderator
Sorry for the code.Using a mvc pattern. I need to access the button's from the view so that I can pass a message from the controller to the model telling it which position was clicked. I create this in the controller, the controller has a reference to the GUI called view. The buttons are public in the view. In my controller constrcutor I pass the ActionListener called al to the view. And then bind the view's actionListener to the button's. Will this let me trigger events from the controller.

//this is how i create the actionListener in the controller
ActionListener al = new ActionListener(){

public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource() == view.BottomLeft){
row = 3;
col = 1;
try {
checkMove(row, col);
} catch (Exception e) {

//this is how I pass it in the constructor =;
//this is how i bind them in the view.
public ActionListener ba;

Thanks for your help.
Campbell Ritchie
Posts: 50644
  • Mark post as helpful
  • send pies
  • Report post to moderator
Yes, but I think you are better off creating a MoveActionListener class which implements ActionListener and allows you to set the row and column numbers.
Then you can provide a static getMoveActionListener(int row, int column) method and call it from the addActionListener methods of the buttons.

Saves no end of messing around with dozens of if-elses.
Gregg Bolinger
Ranch Hand
Posts: 15304
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Justin, please continue this discussion in your original thread on this topic.
    Bookmark Topic Watch Topic
  • New Topic