Is ActionListener in controller for Java GUI App good idea?

后端 未结 4 2022
小蘑菇
小蘑菇 2021-01-01 07:06

I\'t trying to follow MVC pattern. In internet as I see the most famous example is calculator, for example here. I began to use this implementation of MVC pattern. But now I

4条回答
  •  星月不相逢
    2021-01-01 07:54

    I usually follow the following design pattern and it works well for me as it separates the model view and controller quite effectively

    View: Your view should contain all your jcomponents with their getters or setters, component placing related code, layout related code. I never add any listeners to any of the component in the view class, it just deals with the layout

    Model: Model should have placeholders to hold your data for your view, eg: if you have a JTable, model might contain a arraylist that will hold the data for your JTable

    Controller = Model + view (here is where you bind the model with the view) add all your listeners here Bind your text fields, comboboxes etc add your client side business logic add your action listeners for your buttons This is where you should access your view and model.

    Hope this helps.

提交回复
热议问题