Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

java - Dealing with MVC model, jbuttons and ActionListener's getSource() method

So i just stumbled upon this problem while coding my program using MVC. I have a private JButton in the View class. I wrote the method to add the listener to all respective buttons. However, when i'm trying to code the ActionPerformed() part it throws an error about JButton not being visible.

Setting JButton to public solves the problem completly, but is it the right thing to do? Is there another way of setting the ActionListener without making the JButton public?

public class learningView extends JFrame {
    private JButton viewButton = new JButton("View Resources");

    public void addButtonListener(ActionListener listenerForButtons) { 

    viewButton.addActionListener(listenerForButtons);
    saveButton.addActionListener(listenerForButtons);
    addButton.addActionListener(listenerForButtons);

    }
}

public class learningController {

    private learningModel theModel;
    private learningView theView;

    public learningController(learningModel theModel, learningView theView) {

    this.theModel = theModel;
    this.theView = theView;

    this.theView.addButtonListener(new buttonListener());

}

class buttonListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == theView.viewButton) {// This is where problem arises

        }

    }

}

}

Hastebin for view and controller classes (without model) for the convienience. http://www.hastebin.com/ecawolusal.avrasm

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since viewButton has private access to LearningVew, it will simply be inaccessible out of that classes context.

Now, before you change the access level, you might consider changing your approach.

Rather then adding an ActionListener to each button which notifies an external source, it might be simpler to have the view to monitor the buttons itself.

Before you get up in arms over how this would break the MVC, the idea would be to then have the view raise a simpler, more dedicated event for the button in question, for example, viewButton could raise viewWasActivated or something, to which the controller would then respond.

This would require you to define a interface contract for both the view and the controller so they knew what information they were capable of passing to each other and what events might be triggered. This protects the view controls and means you don't need to expose the unnecessarily.

Which is demonstrated in more detail here.

The other choice would be to use the actionCommand property of the buttons instead of comparing references of the buttons to the event source, but you would first need to check that the source of the action event was a button ... and I personally don't like "bulk" ActionListeners, they get messy real quickly...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...