Less than 42 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
    Bookmark Topic Watch Topic
  • New Topic

actionListener help w/sample code  RSS feed

 
Justin Fenety
Greenhorn
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) {
view.displayMessage("text");
if(actionEvent.getSource() == view.BottomLeft){
row = 3;
col = 1;
view.BottomLeft.setVisible(false);
try {
checkMove(row, col);
} catch (Exception e) {

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

Thanks for your help.
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • 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
6
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.
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!