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 3 of 3

Thread: Separate Event Handler Class

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Separate Event Handler Class

    Well, I kind of asked this question here (http://www.javaprogrammingforums.com...ng-styles.html), but back then it was wise to use anonymous classes. And to be honest, it always seemed the wisest move.
    But now I want to have my event handling in a seperate class and I am having a hard time to find the best approach to this. So, suppose I am creating a Swing application. Of course, I should define my variables private. So, while I am defining variables like this:
    	private JPanel contentPane;
    	private JTextField textField;
    	private JTextField textField_1;
    	private JButton btnButtonOne;
    	private JButton btnButtonTwo;
    //Then put the components into container and set their event listeners
    No problems so far. Then I create another class which will be my event handler:
    public class EventListen extends MouseAdapter {
    	private MainWindow listen;
     
    	public EventListen(MainWindow listen) {
    		this.listen = listen;
    	}
     
    	public void mousePressed(MouseEvent e) {
     
    	}
     
    }
    This is where the problem begins. Because I can't access private variables of my MainWindow class (The GUI), I cannot do this:
    public void mousePressed(MouseEvent e) {
    		if(e.getSource()==listen.btnButtonOne){
    			doStuffwithTextFields();
    		}
    		if(e.getSource()==listen.btnButtonTwo){
    			doOtherStuffWithTextFields();
    		}
    	}
    Of course I could just create getters and setters for every single component and variable I need to use.

    However, I think there should be some other method which is more elegant. What would you do if you would have to use a separate event handling class and your GUI (and if the program is complex, your database) variables etc are defined private?

    I thought a lot about it. I could pass the button itself to the listener, but it fails when I want to deal with another component (Like, when button one is clicked, change the text of button two). Honestly, I could not think of a way of handling it except for having getters and setters of every single variable of your class. And sometimes this might be hundreds of getters and setters. And that would make a lots of code.

    What other options do I have if I am using private variables and a separate event handling class?


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Separate Event Handler Class

    I think that having setters and getters for your private instance variables largely defeats the point of having them private.

    I'm not a fan of if(e.getSource==...) chains. I'd attach a listener (inner or other class, anonymous or named) to each of the components in question. Then mousePressed() can do just one thing, like a good method should.

    If the listener is an instance of a separate class then, by assumption, it is going to have to call public methods of the thing it is listening to.

    ---

    If you want do(Other)StuffWithTextFields() to be methods that are separated out from MainWindow consider making a class that contains the text fields in question. That class could expose do(Other)StuffWithTextFields() as public behaviour. And MainWindow could listen for events and call those methods as appropriate.

    If it turns out that the stuff you are doing with the text fields really needs all sorts of information from the main window class, then that could be telling you that the behaviour should really be part of MainWndow.

    ---

    No one way to do anything, of course. So what you might try depends largely on the details.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Separate Event Handler Class

    Quote Originally Posted by pbrockway2 View Post
    If you want do(Other)StuffWithTextFields() to be methods that are separated out from MainWindow consider making a class that contains the text fields in question. That class could expose do(Other)StuffWithTextFields() as public behaviour. And MainWindow could listen for events and call those methods as appropriate.
    Thanks for the tips!

    Do you mean "Provide the textfields from another class into the GUI"?


    Honestly, I still cannot think a way of not using getters and setters. I am not against them and I do not dislike using them.

    The thing is that, I use WindowBuilder Pro(or something like that) to build my GUI, then I fix it if it is needed and add my own code. So, I generally would be stuck with a very big constructor (That is how WindowBuilder plugin works), which includes everything and I do not know but I think this is inefficient. Of course I write many methods outside of the constructor And my question actually just about implementing a seperate listener.

    I hope this is one of those issues which you are stuck at the moment but will figure out easily if you stop thinking about for a while. Because I am out of ideas. Here are some;

    One idea I came up with is, passing everything I need to manipulate to the listener. I mean, the button, the text boxes and any other stuff I need when the particular button is clicked. But I think that would complicate everything, and this should make my life easier.

    The other thing was to pass the button which is clicked. I'd have a Object variable, and if it will be equal to the button clicked. Then again, this will end up being the previous idea, because my text fields are private too.

    I asked similar question in an Android tutorial video series in udemy. I could not understand what exactly the man meant, but he gave me another idea, if he did not mean this. The idea is, I create a type of collection variable (he said HashMap I guess), I add all of my buttons and variables to be manipulated through the listener, and pass that hashmap to the listener. Of course, the rest would be easy.

    It could go like this:
    HashMap<String,Object> hm = new HashMap<String,Object>();
    		hm.put("myKey", myButton);
    		//Put other stuff too
     
    		//Pass it to listener and get myButton using this:
    		hm.get("myKey");
     
    //So I use if statements:
    if(event.getSource() == hm.get("myKey"){
    }

    And of course, if I have tons of variables in the GUI class, I should just make anonymous or at least inner classes, but HashMap sounds nice.

Similar Threads

  1. Search array in separate class ???
    By JGW in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 1st, 2013, 12:45 PM
  2. Replies: 0
    Last Post: June 19th, 2012, 12:58 PM
  3. This Code worked on a separate class but does not work on another class
    By titowinky in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 25th, 2012, 08:48 AM
  4. JButton event handler
    By Jsri in forum AWT / Java Swing
    Replies: 1
    Last Post: October 25th, 2011, 07:02 AM
  5. how to separate this code in another class
    By Jhovarie in forum AWT / Java Swing
    Replies: 2
    Last Post: February 28th, 2011, 06:22 PM