Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

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

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    25
    Thanks
    0
    Thanked 2 Times in 2 Posts

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

    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:

    SudokuModel model = new SudokuModel();
    SudokuView viewController = new SudokuViewController(model);

    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:

    table.addKeyListener(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:

    model2 = model;

    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 model to the event listener, but this doesn’t seem to be possible.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

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

    Your variable "model2" is not an additional Object. Its a variable. And in your case it references the same object as model.
    Whether your solution is ugly or not depends on your definition of ugly. If your controller is supposed to control your model then it makes sense that the controller *knows* the model, that is, saves the model in a variable. Otherwise it would be impossible to manipulate / control it.

    What might be ugly is that its not your controller that is manipulating the model, but in fact, its the Listeners that are manipulating the model. What you can do instead is make the actual Controller manipulate the model with several methods of its own, but these methods are called from the Listeners.
    The end result is the very same, but the idea behind it makes much more sense to me. Listeners tell the controller what is happening and the controller is manipulating the model accordingly.
    But its your decision in the end.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

    Thread moved.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

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

    This might be pertinent: Action Listeners - Static Void Games
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    25
    Thanks
    0
    Thanked 2 Times in 2 Posts

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

    Thanks both! Very useful information!

  6. #6
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    I think the clone() function may be of some use here.

    If a variable points to an object and you do something to the variable, does it affect the object?

    model2 = model;

    Is there such a thing as a pointer in Java?

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

    @Chroot: It's hard to tell if you're giving advice or starting a new topic. You might investigate pointers in Java and whether Java is pass by value, pass by reference, or capable of doing both. If you need more help with those topics, please start your own thread.

  8. #8
    Junior Member
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Lightbulb Re: Chroot - If a variable points to an object and you do something to the variable, does it affect the object?

    If a variable points to an object and you do something to the variable, does it affect the object?
    Yes, it does. For example when you create a new JFrame:

    public class JFrameExample {
     
        private static JFrame frame;                        //<-------- We declare the frame-variable, which soon will hold the JFrame
     
        public static void main(String[] args) {
            frame = new JFrame("Title");                     //<------- We create the JFrame (and add it to the frame-variable)
     
            frame.setSize(400, 300);                      //<------ We set the size of the frame, which affects the object (the newly created JFrame) that is contained in the frame-variable.
        }
    }

    /TheDDestroyer12

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Yes, it does.
    I'm not sure what your example shows.

    You can change the contents of the object without changing the contents of the variable
      SomeClass refToCls = new SomeClass();
      refToCls.changeObject(); // this method can change contents of the object
       //  refToCls is not changed, it still refers to the same object
    And
    You can change the contents of the variable without changing the contents of the object.
    SomeClass anotherRef = refToCls;  // save reference to the original object
    reftoCls = new SomeClass(); //  change contents of refToCls to new object
    //  here the object referred to by anotherRef is unchanged.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    I don't do either. I was too fast to post it. Sorry. Ignore it.

  11. The Following User Says Thank You to TheDDestroyer12 For This Useful Post:

    GregBrannon (October 6th, 2014)

  12. #11
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

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

    What package did you use in the jsdk 1.4.3 or something like that on your web start or JVM at home?

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Please stop these posts on old threads.

    Closed
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. object switching with event listener
    By clavius11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 27th, 2013, 04:30 AM
  2. Event Listener for ComboBox
    By JamesdTurnham in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 9th, 2012, 09:31 PM
  3. [SOLVED] Pass-by-Value scheme, puzzled with passing/assigning a reference to another object
    By chronoz13 in forum Java Theory & Questions
    Replies: 10
    Last Post: June 2nd, 2012, 10:43 AM
  4. Help TEACHING Java
    By ducky123 in forum The Cafe
    Replies: 5
    Last Post: August 3rd, 2011, 01:25 AM
  5. Replies: 0
    Last Post: February 12th, 2011, 07:44 PM