General Java Swing Question

I made a Swing application that is pretty simple in functionality. However, in my opinion, the code that it consists of has become quite large and very dirty. All swing components and actions are in one file. So, for example, if I made an even larger application with more functionality, the code would be pretty hard to get through.

So my question is how to create a good code structure. Or if there is a good web page that I can read about it, and if possible, code examples. I checked out Sun's tutorial on Swing, but these are pretty simplistic examples that they showed.

UPDATE: I reflect on time and check out some examples. I do not know if the MVC template was received correctly. Anyway, my idea is to split each JFrame into its own class file. After that, I have one MainFrame, which is the main window for the application. From this JFrame, I create one instance of each JFrame that I have. And call these frames from MainFrame with Actions. I do not know if this is a good idea. However, this makes the code much easier to read anyway.

Here is an example of how I meant

class Main implements ActionListener { private JFrame frame = new JFrame(); private JButton button1 = new JButton(); private JPanel panel = new JPanel(); private FirstFrame frame1 = new FirstFrame(); private SecondFrame frame2 = new SecondFrame(); private ThirdFrame frame3 = new ThirdFrame(); public Main() { button1.addActionListener(this); } public createGUI() { frame.setTitle("Main"); frame.setSize(400,300); panel.add(button); frame.setVisible(true); frame.setLocationRelativeTo(null); } public static void main(String args[]) { new Main().createGUI(); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == button1) { frame1.enable(); } } } 
+7
java swing
source share
4 answers

Use the Model-View-Controller design template. It focuses on separating user interface code from business logic.

EDIT:

Since the OP is looking for organized code, not flexible design, I removed the NetBeans interface constructor part from my answer.

+8
source share

Streamlining user interface code of any significant size is a problem with surprisingly little written about this, given that every application has the same problem. There are several methods that can help:

  • Refactoring - cleaning up after the code becomes untidy.
  • Model View Controller - split problems.
  • OO Design - Use many small interacting classes instead of large code snippets.
  • "In the case of an action," Action "is an idiom for separating actions from views.
  • UML Component Models - Visualize conceptual concepts above the class level.
  • The principle of dependency inversion is the organization of code dependencies.

The basic design methods have existed for a long time and are well understood, but the application of methods on a wider scale is what seems to be individual for each application.

One approach that I have seen efficiently is to separate Events, Listeners, Actions, etc. into your own classes and either organize them into packages by type, or use a consistent naming convention for packages so that the component and its associated classes are easily identified (XDialog, XDialogMouseListener, XDialogCancelAction, etc.).

Another approach is to look at some large open source applications (Eclipse, Firefox, ...) and see how they organize their GUI.

+5
source share

I am trying to get as much functionality as possible from the Swing components other than the GUI. This means that you have additional indirection. But:

  • Swing level does nothing but interact with the GUI
  • business functionality is much easier to test and therefore maintain

I'm not necessarily talking about the full MVC model (although this is not bad). It is rather a class / package organization problem.

+2
source share

The thing to keep in mind is that user interface programming is like other types of programming. Do not think this is special, and you can ignore reasonable practices. Unfortunately, most codecs are very bad, and production code seems to tend to be much worse.

Key points:

  • Use a layered design. This is not just business perseverance. Split it smaller. For example, a layout layer should not create components (other than a layout, usually JPanel s).
  • Prefer composition for inheritance. There are very few subclasses for points like JFrame and JPanel , so don't do this.
  • Keep the UI thin. For example, renderers should have very little logic. On the one hand, this greatly simplifies testing (automatic testing, large-scale manual testing is counterproductive).
+1
source share

All Articles