简体   繁体   中英

How to share data among swing in java?

I have an swing app, that is used to perform a lot of calculations, making graphs, generate reports etc. In one JFrame, where the data is introduced by a JTable, the calculations are performed and return a Map with the info. Then in other JPanel, another window, this Map must be used in order to execute more calculations. This process must be executed around 6 or 7 window, and in different contexts, I mean not all are in the same JFrame. The question is How can I share this data among the windows? If I were working in a web app, I could put this data on a session scope or something like that and then take it. But how can I perform this in a swing app? Thanks a lot!!!

You can make the parent object hold all of the data and create setters and getters for it. Each window would then call get/set to get access to the data.

You can also do some research into the "Model-View-Controller" pattern. You have your parent object hold all of your data objects. Then you create windows that display the data using 'get' request methods from the parent, and controls that update the data with 'set' methods.

The MVC architecture is the answer. Keep your data and your business logic in java classes that have nothing to do with swing and don't even know swing exists. Then pass this data around to your Swing code. The ActionListeners call the business logic that modifies the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM