How do you regenerate autogenerated code in NetBeans? - java

Create a project in NetBeans and create a new JFrame.
Use the GUI Builder to drag some components like a button or label onto the frame and look and the source. You'll see by default that the member variables are private in the frame class.
Now go to Tools -> Options -> Misc -> GUI Builder and change something like the variables modifier to protected instead of private.
Now how do you apply those changes to the already generated code? I've tried several things like format code, fix code, etc. I've even tried cutting all the components off of the frame and then repasting them hoping to fix the issue, but it still uses the old settings.
When I create a new JFrame in the project and perform step 2 again, the changes have taken effect. Any new code generated on a new frame or file works as expected, but not the original.
This is very strange behavior, and I have to imagine there's an easy straight forward way to regenerate this code. Am I missing something?
I'm using NetBeans 7.1 and Java 7u2. Thanks in advance!

As you have already alluded to, the GUI Builder options are defaults only, for the creation of the form.
You can change most things about already-generated GUI elements.
To change the GUI components 'access' from private to protected, right-click the component in the GUI designer and select "Customize Code". At the bottom of the "Code Customizer" dialog you can change just about any aspect of the declaration of the GUI element. That dialog also lets you customise things like the constructor used for the element.
I would recommend you leave the access default at private, and only change the elements that you really need to be protected or even public.
And don't listen to the doom-sayers. We have over 600 GUI-designed forms in our application, we use the GUI designer every day, with multiple developers, and we very rarely have any issues at all.
By the way, we are using version 6.9.1 of NetBeans with Java6, so YMMV.

Related

Add extended control to form - NetBeans

I'm new to Java, so need a little bit help:
Programming in Java I use NetBeans. So, in making forms, I use already existing swing controls just placing them on the form. But, for example, I want to improve control as a point add some new action listeners, so good solution would be override it.
I can create new class and write:
public class ExtendedControl extends Control
{
}
But, is it possible to add ExtendedControl to form automatically (like original controls)?
You will need to write your own JavaBean Components. This can be easily done with Netbeans.
Once you are done you can add your JavaBean to the Beans Folder, or wherever you wish, in the Palette Manager.
Here is the manual and here is an example that shows you how to proceed.
These are the few steps, necessary in order to add to the Palette.

Netbeans GUI Builder Private Members

I have an existing project for which i have decided to create a GUI for in Netbeans. The problem I am encountering is the fact that every component that i drag-and-drop is private in the source and is unmodifiable. Must i create getters for everything?
I mainly just need this problem resolved for appending to the TextArea.
Thank you in advance
If you want to change that globally, go to the options dialog, then select miscellaneous, and pick the gui builder tab. You can configure the default modifier there.
By right clicking on a component in the Inspector panel, you can influence the generated code, even though it is in an editor-fold and not directly editable. For example, right click on a JList and edit the Properties > model to add text entries; right click on Code > Post Creation Code to add a code snippet affecting the selection model:
itemList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
Examine the code in the editor-fold to see the generated changes.
See also Introduction to GUI Building.
You should see some tags in the code, something to do with begin and end of variable area. Usually there are 2 different sets of tags, any code between those tags will regenerate when you modify the gui with the form builder.
You can write your own code outside of those tags and it should remain even after you make changes. Getters and setters are a good idea if you need to update your object from another class. I've done that before with some text areas where I had a utility class update the text in it.

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 (e.g 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.
Members defined by us that are relevant to the application logic moved to the newly created subclass.
Components access modifier made "protected" (they are "private" by
default). To do so: Right click -> Properties -> Code (tab) -> Set
"Variable modifier" to "protected"
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
}
});

How do I add components at run time to a Swing UI created with Netbeans visual editor?

I am currently writing an application where the user has, at some point, to click a button which have been generated at run time. I know how to do it when writing all my swing code from scratch, but I'd like to take advantage of Netbeans' visual editor.
The generated UI code goes into an initComponents() method I can't modify since it is regenerated automatically from the visual form.
I'd like to have a panel I place at design time using the visual editor in which I could add the buttons at run time so that they fit nicely in the layout, but I don't know how to access the panel in a convenient way. Besides, there may be another method than using a panel.
So basically :
How do I locate a Swing component at run time ?
Is there a better way of integrating components created at run time in a generated Swing UI ?
Thanks for your help.
NetBeans-generated GUI classes store all the components in private variables. You can add a method into the generated class that returns the panel, and it will remain even as you do additional design.
If you're going to use a generated UI, then it's probably best to use a JPanel within that UI to "carve out" space for your own components. Otherwise, you'll have to worry about how your components affect the layout of the components placed by the UI.
Just because you are using NetBeans generated GUI classes doesn't mean that you have to use the Group layout for the panels. I find that switching it to a BorderLayout helps especially in cases where I want to add some dynamic user interface code.
It is possible to change private to protected/public by either right clicking on a component in the GUI-Designer, choosing properties and hitting the Source-tab or right clicking on a component and choosing "Modify Source" (or something like that) and setting the appropriate access modifier.
Or just export them via a getXYZComponent() method.
Locating the component should provide as being too difficult, as you built it with the designer and thus know each component.
For example, if you had a JTabbedPane and wanted to add tabs to it when the user hits a button or something like that, you would simply issue myJTabbedPane.add(myCustomComponent); et voila, a new tab appears.
It is also possible to modify the auto-generated code and/or the code used for auto-generation by using the "Modify source" dialog mentioned above, which can be really useful.

Netbeans 6.7.1 mainPanel resizing problem

I've created a small gui app in Netbeans. As I was adding in some buttons and text areas the mainPanel resized itself. Now it is really wide [probably 4x as wide as I want] but when I try to drag the edge in it won't resize back down. If I drag it out, making it bigger, it takes that change. I would just like to return the mainPanel back to a reasonable size. Not sure what I'm doing wrong here. I've tried to change the min size, max size, and preferred size settings for the mainPanel with no success. I've even tried to change the menuBar & statusPanel settings at the same time as the mainPanel [thinking that one of them was making the others too big] without success.
Any ideas?
Netbeans does do really stupid things like that sometimes, and I generally get around them using either of these two methods:
First thing to try is to change the layout used. Try the Grid Bag Layout, or any of the others and see if you get better results.
If that doesn't work, then probably the easiest thing to do is to change stuff in the code. You will notice that Netbeans automatically adds a call to initComponents(); in the constructor (you have to switch to Code view from Design view). And if you look at initComponents, it will have a whole heap of auto-generated code to create the GUI. Do NOT edit this, because it's just a matter of time before Netbeans overwrites your changes. What I do is to create a new method initComponentsFix, and call that immeidtaely after initComponents in the constructor. In initComponentsFix, I would add the code to resize the component to the preferred size, and any other things you you want to fix.
BTW I empathise with you - Netbeans' GUI editor is still in need of much work. However, it's code auto-generation is still very useful, so I wouldn't recommend coding the GUI the good ol' fashioned way. That's why I'm advocating using it up until you start felling its limitations, after which you "take control".
There is also a third way, which I would not recommend, is to edit the file that Netbeans stores the Design view in, which is basically shares the same file name as your frame's class' source code, except with a .form extension.
This file is XML, and is pretty easy to edit. I don't recommend this because it is sorta going around the back door, but as a last resort, you can still try it.

Categories