HACKER Q&A
📣 rlawson

Tips for modern Java Swing development?


I'm writing some utilities with Java Swing (I'm a Java dev working in a Java shop). Been away from it a while - what is the best app framework? I have settled on flat laf (https://www.formdev.com/flatlaf) but what about MVC/app support? I found Griffon. Anything else?


  👤 jeffreportmill1 Accepted Answer ✓
I think the biggest flaw of Swing is that it didn’t provide or promote a standard ‘Controller’ class. I have seen tons of Swing code over the years that is a spaghetti code mess of UI creation, initialization, updating and responding.

So my big tip is to create a SwingController base class for any of your UI panes with the following methods:

  - JComponent createUI();
  - void initUI();
  - void resetUI();
  - void respondUI(Event anEvent);
Theres a bit more to it than that, since you want resetUI to update all of your components without triggering respondUI(). And you want all your components to be automatically configured to call respondUI() when there is user interaction.

I’ve written one of these before, but I don’t have access to a public version anymore. I do all my current UI dev in a UI kit built on top of Swing. But here is what I use there that solves this problem:

https://github.com/reportmill/SnapKit/blob/master/src/snap/v...


👤 pxcse
Is there still demand for Swing development? I would have assumed JavaFX is the way to go.