Results 1 to 2 of 2
  1. #1
    _Mario_ is offline Member
    Join Date
    May 2014
    Posts
    1
    Rep Power
    0

    Question MVC Layout - Which way to add listeners is better?

    So I'm doing a basic MVC layout for a pretty basic game that I am making in order to understand the whole MVC layout. The game requires the user to move up/down/left/right via JButtons on the GUI. Since I'm using an MVC layout and my buttons are in a different class than the ActionListeners, I was wondering what the best way to add the action listeners are?

    Method 1:

    View Class ActionListener method:
    Java Code:
    public void addMovementListeners(ActionListener u, ActionListener d, ActionListener l, ActionListener r){
        moveUp.addActionListener(u);
        moveDown.addActionListener(d);
        moveLeft.addActionListener(l);
        moveRight.addActionListener(r);
    }
    Control Class add ActionListener method:
    Java Code:
    private void addListeners(){
        viewGUI.addMovementListeners(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                buttonPressed = 1;
            }
        },
        new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                buttonPressed = 2;
            }
        },
        new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                buttonPressed = 3;
            }
        },
        new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                buttonPressed = 4;
            }
        });
    }



    Method 2:

    View Class ActionListener method:
    Java Code:
    public void addMovementListeners(ActionListener a){
        moveUp.addActionListener(a);
        moveDown.addActionListener(a);
        moveLeft.addActionListener(a);
        moveRight.addActionListener(a);
    }
    
    public JButton[] getButtons(){
        JButton[] temp = new JButton[4];
        temp[0] = moveUp;
        temp[1] = moveDown;
        temp[2] = moveLeft;
        temp[3] = moveRight;
    
        return temp;
    }
    Control Class add ActionListener method:
    Java Code:
    JButton[] movementButtons;
    
    private void addListeners(){
        movementButtons = viewGUI.getButtons();
        viewGUI.addMovementListeners(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == movementButtons[0]){
                    buttonPressed = 1;
                }else if(e.getSource() == movementButtons[1]){
                    buttonPressed = 2;
                }else if(e.getSource() == movementButtons[2]){
                    buttonPressed = 3;
                }else if(e.getSource() == movementButtons[3]){
                    buttonPressed = 4;
                }
            }
        });
    }



    Which method is better? Is there another method that is even better than these two? Let me know! Trying to get this MVC thing down :). (Any other suggestions welcome as well)!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: MVC Layout - Which way to add listeners is better?

    Well, for action listeners, you can also have a single listener and then use the action Command to handle the appropriate action. It is not necessary to have a separate action listener for each component.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Listeners help!!
    By Luwine in forum New To Java
    Replies: 3
    Last Post: 06-18-2013, 05:52 PM
  2. Grid Layout change layout alignment of control s
    By rellicott in forum SWT / JFace
    Replies: 1
    Last Post: 02-13-2012, 05:11 PM
  3. two different Listeners
    By Billaguana in forum New To Java
    Replies: 8
    Last Post: 01-21-2011, 03:20 AM
  4. Edit layout Layout please help me
    By manhtungtnk28@gmail.com in forum New To Java
    Replies: 4
    Last Post: 11-23-2009, 08:41 AM
  5. Seriously need help on my listeners!!
    By themburu in forum Java Applets
    Replies: 4
    Last Post: 05-26-2008, 10:41 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •