What is the controller in Java Swing?

前端 未结 7 808
不知归路
不知归路 2020-12-23 10:38

I would like to apply the MVC design to my Java application using Swing in a meaningful way. Therefore my question is, how the controllers would be structured in Java Swing?

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-23 11:13

    While the Swing framework already implements a form of MVC (explicit models; JXyz & UI classes = controller & view), this strict separation is rarely used on application level and looks rather weird.

    To start I suggest to follow the following design:

    • implement the client-side business logic with POJOs
    • wrap the POJOs with custom Swing models where needed (ListModel, TableModel)
    • Use a GUI builder to design the GUI
    • Use the Mediator pattern to listen for events (the custom parent JPanel listens for events of its children and updates other children or fires own events if needed)

    If you want to go a step further, use a RCP such as the NetBeans Platform (very recommended).

提交回复
热议问题