• Post Reply Bookmark Topic Watch Topic
  • New Topic

Teaching myself Java – how to pass on an object to an event listener?

 
Evert DeBoer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I’m teaching myself Java. I am a fairly proficient programmer in other languages, but this is the first OO language I’m doing.

I have a question that is a bit hard to summarize. Or it should be: how can I pass on an object (or variable) to an event listener?

I am writing an application in which you can play a Sudoku game. I have separated the “logic” or the “model”(the classes with Sudoku data structures and methods to manipulate them) from the presentation (the view and the controller).

The main method starts off as follows:



The first line creates class for the logic and the second line creates the class for the view and the controller. Since the view and the controller need access to the business logic, the model is passed on to the ViewController class.

The SudokuViewController class creates the user interface in Swing and it handles the user input. For the user input I have created a number of listeners, like this:



Now these listeners need access to the model since they update it. However, as far as I’m aware the only parameter passed on to an event listener is the event itself. So these event listeners do not have direct access to the model, even though it is passed on to the constructor of the class SudokuViewController.

To circumvent this, I made model2 an attribute (variable) of the class SudokuViewController. The constructor of the class sets this variable as follows:



Now the event listeners have access to model2, which they can manipulate.

This works. However, I think it is an ugly solution, introducing an additional object (model2). How can I solve my problem without doing so? I’d like to pass on the object named model to the event listener, but this doesn’t seem to be possible.
 
Knute Snortum
Sheriff
Posts: 3261
84
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not creating another object, you're creating another reference to the object. There is virtually no overhead involved. What you've come up with is actually very standard and is called composition. The view controller "has-a" model, so you create a reference to it.
 
Campbell Ritchie
Marshal
Posts: 52576
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We usually discuss Listeners in the GUIs forum: moving discussion.
 
Campbell Ritchie
Marshal
Posts: 52576
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean the first line creates an object from the class ... and the second line creates an object of the other class. You don't usually create classes in code. You usually create them with a text editor and now you are using those classes.

Your puzzle should be separate from the GUI. You should have classes which you create objects of which display the puzzle. Also classes whose objects solve the puzzle. The puzzle solving classes should have an interface which you can use. Another way to say that is there should be a model with a public interface. One way to do it is like this:-In my opinion the one thing you should not do is make the enclosing class a Listener. You will need a different Listener for each button and one large method is really difficult to maintain. If the Listeners are created in the constructor like that, they will have access to the sudoku reference. Or you can pass the sudoku reference to their constructors.
 
Evert DeBoer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all! Very useful information!
 
Campbell Ritchie
Marshal
Posts: 52576
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!