0

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...

mKorbel
  • 109,525
  • 20
  • 134
  • 319
reox
  • 5,036
  • 11
  • 53
  • 98
  • @reox I amended your post, please revert if isn't... – mKorbel Jun 22 '11 at 11:57
  • 1
    @reox, Focus on encapsulation. And you're right, using `SwingWorker` to handle long-running tasks exemplifies "best practice", but it's not necessary. There's other utilities (e.g. `SwingUtilities`) that you can use to ensure that Swing-related ations are posted on the `EDT`, but occur in a separate thread. Perhaps providing a small snippet of code would help us refine our answers. – mre Jun 22 '11 at 12:00
  • @mre: so i should use a factory and should make all my fields and stuff encapsulated? – reox Jun 22 '11 at 12:06
  • Consider to extends AbstractAction instead of implementing ActionListener interface. – Heisenbug Jun 22 '11 at 12:08
  • yes i did, i have an own Action class, which contains my Swing Worker stuff, so when i call the method .runBackground() it will start a new swing worker with the content of myAction.process(args) - so I'm on the right way? – reox Jun 22 '11 at 12:12
  • reox http://stackoverflow.com/questions/6171414/how-to-share-data-with-two2-swingworker-class-in-java/6186188#6186188 – mKorbel Jun 22 '11 at 12:27
  • You should avoid using `KeyListener`s whenever you can (which is in 99.9% of situations), and use `InputMap` and `ActionMap` instead. – jfpoilpret Jun 22 '11 at 20:42

1 Answers1

2

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 Actions 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).

jfpoilpret
  • 10,449
  • 2
  • 28
  • 32