How to refactor NetBeans generated GUI code?

I created the GUI in Netbeans through the Netbeans Swing GUI creator. So I just dragged Swing Components from the palette window and all the code was created by netbeans.

Now the code is too long to support (about 10,000 lines). Therefore, some SO experts suggested that I reorganize my code.

I can reorganize the code that I generated, but I don’t know how to reorganize the code generated by Netbeans, since it does not allow editing its generated code.

Any suggestions?

+4
source share
5 answers

10,000 lines of code sound like you have everything in this one class.

Start by splitting your source into Model, View, and Control ( MVC ).


You can also extract some JPanels into separate classes. One way to do this is to create a new JPanel (new file) and cut / paste components from one main panel into the new JPanel . Save and compile the new panel.

Then go back to the main frame, select Beans β†’ Choose Bean from your Palette and select the newly created class (for example, com.example.YourPanel ).

Before you try, make sure you have a backup of your application.

+8
source

Well - if the code is generated, I do not see any advantages in refactoring it, as long as the tool generated by it can handle this.
The tool (that is, the designer in this case) will β€œdestroy” all your refactoring work as soon as it updates the code.

However, you must split Control / Window / ... into several controls - then the code will be automatically shortened and you can more easily maintain your interface.

As a conclusion: not refactoring the generated code, but refactoring your control.

+2
source

Manual GUI code with layout managers.

Using the GUI builder tools makes refactoring GUI code nearly impossible. I have to use these idiotic Intellij Swing GUI design forms. Now I can’t even rename my packages in Eclipse because it will not be updated in the forms.XML file.

Stay away from GUI developers. If you want to create really complex, supported GUIs, then do it manually using GridBagLayout and everything else.

+1
source

If you need to use netbeans due to project limitations (for example, the rest of the team, or requirements for it), use Matisse to break up the huge shape into smaller panels, each of which the designer can edit. You can do this by creating a new shape and cutting and pasting panels from the large shape into the new shape.

But at the same time, make sure that all business logic is moved from user interface classes.

If you don't need to use matisse / netbeans, you can open the project in Eclipse and edit the forms with WindowBuilder, it will do it in real Java code instead of an uneditable form so that you can then slice and edit it for your heart.

0
source

You can extract the application logic into a separate subclass. Then use the subclass directly. I managed to execute the following method.

  • The members we define that are relevant to the application logic are moved to the newly created subclass.
  • Modifier of access to components made "protected" (they are "private" by default). To do this: Right-click β†’ Properties β†’ Code (tab) β†’ Set "Variable modifier" to "protected"
  • Event handling methods moved to subclass. When you add events to a component using the property bar, it changes the initComponents () function, adding the appropriate code, as in the following code example. Here, the definition of btnNum6ActionPerformed () is added to the empty body class. Unfortunately, btnNum6ActionPerformed () is private and does not have the ability to change the access modifier using NetBeans. Therefore, they cannot be redefined. To get rid of this, you can define another intermediate function and call it inside btnNum6ActionPerformed (). It is best to make the base class and its intermediate event processing functions abstract.

     btnNum6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnNum6ActionPerformed(evt);//Definition of this method is added too } }); 
0
source

Source: https://habr.com/ru/post/1299190/


All Articles