简体   繁体   中英

How to refactor Netbeans generated GUI code?

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

Now the code is too long to maintain (approx. 10,000 lines). So some experts on SO suggested me to refactor my code.

I can refactor the code that was generated by me but I don't know how to refactor the code generated by the Netbeans as It doesn't allow editing in its generated code.

Any suggestions?

10.000 lines of code sounds like you have everything in that single class.

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


You might also be able to extract some JPanels into separate classes. One way to do this is to create a new JPanel (new file), and cut/paste your compoments from one main panel into that new JPanel . Save and compile your new panel.

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

Make sure to have a backup of your application before you try this.

Well - if the code is generated, I don't see any advantages in refactoring it as long as the tool which generated it can handle it.
The tool (meaning the designer in this case) will "destroy" all your refactoring work as soon as it updates the code.

However, you should split your Control/Window/... into multiple controls - then the code will automatically get shorter and you will be able to maintain your UI more easily.

As a conclusion: Do not refactor the generated code but do refactor your control.

Handcode the GUI code with layoutmanagers.

Using GUI builder tools, makes it nearly impossible to refactor GUI code. I have to use these idiotic Intellij Swing GUI designer forms. I now cannot even rename my packages in Eclipse because it wont be updated in the forms.XML file.

Stay away from GUI builders. If you want to build really complex, maintainable GUIs then do it by hand by using GridBagLayout and all the rest.

If you have to use netbeans, because of project limitations (eg the rest of the team is, or requirements say to) then use Matisse to break up the huge form into smaller panels, each of which the designer can edit. You can do that by creating a new form, and cutting and pasting panels from the big form into the new form.

But at the same time, make sure all the business logic is moved out of the UI classes.

If you do not have to use matisse / netbeans, you can open the project in Eclipse, and edit the forms using WindowBuilder, it will do it in real java code instead of the uneditable form, so you can then chop and edit it to your heart's content.

You can extract the application logic into a separate subclass. Then, directly use the subclass. I succeeded with the following method.

  1. Members defined by us that are relevant to the application logic moved to the newly created subclass.
  2. Components access modifier made "protected" (they are "private" by default). To do so: Right click -> Properties -> Code (tab) -> Set "Variable modifier" to "protected"
  3. Event handling methods moved to the subclass - When you are adding events to a component using properties pane it changes initComponents() function by adding the relevant code like in the following code sample. Here definition of btnNum6ActionPerformed() is added to the class with an empty body. Unfortunately btnNum6ActionPerformed() is private and no way to change the access modifier using NetBeans IDE. Hence, they cannot be overridden. To get rid of this, you can define another intermediary function and call it inside btnNum6ActionPerformed(). It is better to make the base class and its intermediary event handling 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 } }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM