简体   繁体   中英

Best practise for GUI Components ActionListener?

I did a big GUI Based App and I have now many many Action stuff around there... I have different Listeneres , like ActionListener , KeyAdapters , ... Everything should run threaded, so my GUI isnt freezing when do long time operations. So im using SwingWorker in every ActionListener ... Currently its working like this: I have my JComponents , bound on one single ActionListener . In this Listener I decide what to do, based on the actionCommand() . The I call a method, which contains my SwingWorker and the Action which should be performed.

Ii dont like this concept anymore, because my class is getting longer and longer and i dont have an overview about all the functionallity. So I decided to do it another way... I thought Factory Methods would be a great thing, but here is my problem with them: I need sometimes data from my GUI, eg: when pressing JButton x, what is in JTextField y and so on... so what is the best practise for this? Should I just give my Factory a instance of my complete GUI? Or a ArrayList of Components ? Another problem is that need to change values from my GUI, eg: press button x and then filter a JTable ... how should i do that? doing it like this = myFactory.process(this); isnt really that what i want...

Take a look at JGoodies Binding : it emphasizes the use of PM ( PresentationModel ) where all the GUI state is stored (and bound to the actual GUI components).

Every View has an associated PM that makes the link with the domain model. PM can live without the View (but the reverse is not true).

PM should not have any GUI-related dependency (so that it is unit testable without the GUI), hence no reference to a JTextField, JButton...

However, PM normally includes Action s that are attached to buttons from the View . Actions are not actual GUI components (although they belong to javax.swing package.

I said "normally" because some Actions may need to display a message box, open a new window... This kind of actions should then be put in another class. Note that Karsten Lentzsch (JGoodies author) doesn't talk about this case in his presentations (this is my own way of dealing with this case).

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