Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

First Swing Project

 
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Are there any tips anyone can give, or resources or guidelines to look up, which can help me build a Swing desktop app using good MVC approach? So I can keep the Swing and Logic kind of separated?

Many thanks in advance.
Jason



 
Marshal
Posts: 5218
323
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd probably recommend you skip Swing and move right along on to JavaFX which is its successor.
 
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:I'd probably recommend you skip Swing and move right along on to JavaFX which is its successor.

I'm not so sure I'd make that recommendation. Swing is mature. Yes, JavaFX was destined to become it successor, but from various discussions it sounded like they pulled the plug on it before it fully matured.  I am probably speaking out of turn here because I don't have any direct experience with JavaFX but I have paid close attention to any discussions comparing Swing and JavaFX. YMMV.
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding MVC, I was struggling with how to implement that in a way that would scale nicely without hand coding all the relationships, which I thought made the design brittle. Then I ran accross JGoodies Bindings at one job and was amazed at how elegant it was. You could make as many models as you wanted and views as you wanted and cross connected them with observers, then, change one and the others update.

Probably a pay to play though.
 
Saloon Keeper
Posts: 24822
172
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I'm not really in touch with that sector (few Java systems are based on a desktop GUI, although some really good ones are).

But I've been hearing the same deprecation of JavaFX myself.

On the other hand, those are only two GUI frameworks, albeit ones blessed as official Java. The Eclipse IDE, for example, uses a GUI framework called SWT as does the Hitachi Pentaho product line. I've worked with both SWT and Swing and I'm not really sure what the appeal for SWT is, but there it is. Of course some of that might be because SWT has been around so long that it might pre-date Swing and nobody wanted to work with AWT if they could help it. Eclipse is a very venerable product.

I have a nice thick book on Swing, but I think it's dated 2003. The most recent stuff that I can easily find is 2015, although I'm not sure Swing has really changed that much over the years anyway.
 
Antonio Moretti
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Our teacher said that JavaFX has become more niched since Oracle removed it from the Java Standard Library and that Swing is still widely used. Given the limited time we have to study desktop stuff, Swing seemed the more relevant choicie and has good IDE support. JavaFX is not properly integrated in Netbeans yet, although with some tweaking we can get it to work.
 
Ranch Hand
Posts: 517
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In terms of Swing and MVC, think in terms of events.

A user pushing a button is an event . A user entering or leaving an edit box is an event.
Something(or nothing) happens as result of the event.

That event calls an action.
That action does something with the data in the form.

You might want to have classes that handles different types of actions -That would be your controller.
That controller then might pass the form data onto another class (the model) class to validate the the data.
The form holding the button and edit boxes is the view

These three things can/should be tested independently of each other.
You want them to be loosely coupled ie not dependent on something happening on one layer for something in the other layer to work.

So you want to keep as much java business code out of your forms(views) as possible .
Use the form to gather and display the data and have it interact with model classes that do the heavy lifting.

Think of it like you would if it was a web app .

A web app  =  jsp/html page-> controller/servlet code -> model code validates the form that  the controller passed to it/other business logic -> data persistence retrieval -> Web server sends a response back to the browser.

A swing/console app= form -> validates the form other business logic -> data persistence retrieval -> You (not a webserver) respond back to the view layer of the application .

The main difference is what happens with the controller classes. In a web app it routes to another a page or deals with the data and sends a response back to the browser .
In a console/Swing app the event is what calls a controller action that action then processes the event , models any data passed to it . It then processes the model data then sends a response back  to the form application .

Hope this helps.
Paul
 
Antonio Moretti
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

paul nisset wrote:In terms of Swing and MVC, think in terms of events.

A user pushing a button is an event . A user entering or leaving an edit box is an event.
Something(or nothing) happens as result of the event.

That event calls an action.
That action does something with the data in the form.

You might want to have classes that handles different types of actions -That would be your controller.
That controller then might pass the form data onto another class (the model) class to validate the the data.
The form holding the button and edit boxes is the view

These three things can/should be tested independently of each other.
You want them to be loosely coupled ie not dependent on something happening on one layer for something in the other layer to work.

So you want to keep as much java business code out of your forms(views) as possible .
Use the form to gather and display the data and have it interact with model classes that do the heavy lifting.

Think of it like you would if it was a web app .

A web app  =  jsp/html page-> controller/servlet code -> model code validates the form that  the controller passed to it/other business logic -> data persistence retrieval -> Web server sends a response back to the browser.

A swing/console app= form -> validates the form other business logic -> data persistence retrieval -> You (not a webserver) respond back to the view layer of the application .

The main difference is what happens with the controller classes. In a web app it routes to another a page or deals with the data and sends a response back to the browser .
In a console/Swing app the event is what calls a controller action that action then processes the event , models any data passed to it . It then processes the model data then sends a response back  to the form application .

Hope this helps.
Paul



Very much so. Thanks!
 
Tim Holloway
Saloon Keeper
Posts: 24822
172
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Events and Actions are the heart of Swing. It's important to note that Actions can be triggered by more than one type of event.

For example, the "File Save" action may be triggered by a menu, by the F2 function key, by Ctrl-S, by an internal or script command (many GUI apps can be run alternatively via batch scripts), by a toolbar button, and I've probably forgotten a few. For example, via a dialog.

So instead of re-implementing the same (file-save) logic over and over again, you invoke file-save from the File Save Action and tie the various trigger events to that action. Conversely, many GUI controls should be informed when an Action is not possible or at least useful. So, for example, if you've just saved a file and not yet made any changes, the menu, button and other controls should disable (ghost).
 
Saloon Keeper
Posts: 13481
304
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A short word on Swing vs JavaFX:

People keep saying that JavaFX has been dropped, but this is far from true! Oracle intended to replace Swing with JavaFX, but people just weren't interested. However, instead of abandoning JavaFX, it was moved to an open-source project OpenJFX, where it is alive and kicking. Development is very much active.

This is neither a case of "JavaFX is meant to replace Swing, so use JavaFX", or "JavaFX was abandoned, so use Swing". Both are very good choices to build desktop applications with, and the choice comes down to preference.

JavaFX has a more modern declarative style of writing user interfaces, where you use FXML to lay out your user interface, and Java to control them. This is similar to HTML + JavaScript for web pages.

Swing has an older procedural style, where you use Java to tell the system how to build the user interfaces.

Normally I'd tell people to use JavaFX because it has a clearer separation of concerns (Swing UIs written in Java can be a real mess), but JavaFX's bloated mess of properties is really not that fun to work with. There are also a lot of small use cases that are not easily implemented using the JavaFX API, that sometimes just make Swing easier to use.

It boils down to this: If you haven't worked with either Swing or JavaFX before, and prefer a clean separation of layout and event handlers, pick JavaFX. If you are already comfortable with Swing, or you don't feel like learning FXML, pick Swing. There really is no bad choice.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic