Saturday, 15 January 2011

java - Should I create Swing components within Actions? -



java - Should I create Swing components within Actions? -

i creating actions swing components, however, need generate confirmation jdialog. should creating jdialog within action itself, should pass in action, or there alternative method?

example

public static class create extends abstractaction { @override public void actionperformed(actionevent event) { int selection = joptionpane.showconfirmdialog(...); // ... processing } }

this illustration quite hard unit test since involves automating selection of jdialog (which feels code smell).

solution?

public static class create extends abstractaction { private jdialog dialog = null; public create(jdialog dialog) { this.dialog = dialog; } @override public void actionperformed(actionevent event) { dialog.display(); // ... processing } }

how this:

class dialogs { public static dialogs instance = new dialogs(); public int confirm(string message) { homecoming joptionpane.showconfirmdialog(null, message); } }

then phone call dialogs.instance.confirm() everywhere phone call joptionpane.showconfirmdialog(). testing, set dialogs.instance mocked value.

java swing user-interface

No comments:

Post a Comment