Netbeans GUI editor generating its own incomprehensible code
You may have inadvertently selected Java Desktop Application
Creates a skeleton of a desktop application based on the Swing Application Framework (JSR 296). This template provides basic application infrastructure such as a menu bar, persisting of window state, and status bar. With this template, you can also generate code to create a GUI interface for a database table.
Rather than Java Application
Creates a new Java SE application in a standard IDE project. You can also generate a main class in the project. Standard projects use an IDE-generated Ant build script to build, run, and debug your project.
Addendum: Use File > New File > Java GUI Forms
to add high-level containers, e.g. an enclosing JPanel
, that can be instantiated from main()
's run()
method.
For example, Main.main()
:
package temp;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new NewJPanel());
f.pack();
f.setVisible(true);
}
});
}
}
And a NewJPanel
built in the GUI editor (note "Generated Code"):
package temp;
public class NewJPanel extends javax.swing.JPanel {
/** Creates new form NewJPanel */
public NewJPanel() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel1.setText("Hello, world!");
org.jdesktop.layout.GroupLayout layout =
new org.jdesktop.layout.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(163, 163, 163)
.add(jLabel1)
.addContainerGap(157, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(113, 113, 113)
.add(jLabel1)
.addContainerGap(171, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
Netbeans GUI editor problems
Do a "Clean and Build" on your project and run it again. I've had the same problem. The GUI builder in NetBeans stores the text for your components in a properties file. New properties don't seem to be loaded properly when you just run the project in the IDE until you do a fresh build.
Netbeans history shows 100 changes but I made only a few. How do I avoid this?
The folded code is regenerated, based on GUI editor properties, each time you build; the effect you describe is not unexpected. Alter a specific property, e.g. by using the Customize Code…
dialog, and see how the generated code changes. While the source code editor can collapse generated code, the history views cannot.
Some mitigation strategies:
Limit use of the GUI editor to the relatively small number of enclosing containers that really need it, as shown here.
Use the
History View
navigation controls to move among available changes.Identify critical revisions in the
Message
column of theDiff Viewer
pane.Check
Options > Team > History
settings to match your workflow.Search for
History View
in the help dialog for additional guidance.
How to modify/add code to the initComponents() method in Java using NetBeans?
The initComponents() method is regenerated by the IDE as you create your UI in the GUI editor. The method is 'guarded' to prevent this regeneration from overwriting user written code.
There are a couple of ways to add code to this method, indirectly:
Drop a new component onto the design
editor 'canvas' for the window.Enter code as part of one of the
following code properties:
Pre-Creation Code, Post-Creation
Code, Pre-Init Code, Post-Init Code,
Post-Listener Code, Pre-Population
Code, Post-Population Code and
After-All-Set Code.There are a couple other code properties that do not alter the initComponents() method... but can be very useful: Pre-Declaration Code and Post-Declaration Code.
alt text http://blogs.sun.com/vkraemer/resource/code-properties.png
Note: the editor for these properties is not 'rich', so I would recommend creating methods in the "regular editor" that you just call in the initComponents().
You can modify the code in the initComponents() method by positioning or changing the 'regular' properties of the 'base panel' or controls.
GroupLayout autogenerated code in NetBeans
Kudos for embracing the NetBeans GUI designer as a means to—and not a substitute for—understanding Swing. Summarizing the comments,
While
GroupLayout
was designed for automated code generation, it can usefully be used manually, as shown here and here. It can also be integrated into the mixed development approach suggested here.Experienced developers wisely suggest learning one or more popular third-party layouts such as
MigLayout
,FormLayout
orDesignGridLayout
, which derive some power from accepting human-readable text parameters. I seeGroupLayout
in the same category, but simply having a fluent interface.In your example, the two layouts have differing resize behavior, which may impact other choices. Beware of this common pitfall.
Automatic updating of custom graphical components in Netbeans graphical designer
The NetBeans GUI designer doesn't support this directly, but you can instantiate your own custom components as often as desired. The key is designing for reuse. I try to follow the Swing separable model architecture, even if there's no need for a custom look & feel.
Edit: I added two instances of NewJPanel
to Main
like in this example. When I changed the label's text in the designer, it changed in both panels at runtime.
f.setLayout(new GridLayout(0, 1));
f.add(new NewJPanel());
f.add(new NewJPanel());
Related Topics
Close a Scanner Linked to System.In
Initialization of an Arraylist in One Line
Java - Escape String to Prevent SQL Injection
Java Hashmap: How to Get Key from Value
Difference Between Declaring Variables Before or in Loop
What Does the ^ Operator Do in Java
Bufferedwriter Not Writing Everything to Its Output File
How to Concatenate Two Strings in Java
How to Run Eclipse in Clean Mode? What Happens If We Do So
What's the Difference Between @Component, @Repository & @Service Annotations in Spring
What Are the Effects of Exceptions on Performance in Java
Does a Lambda Expression Create an Object on the Heap Every Time It's Executed
How to Pretty Print Xml from Java
Difference Between Javac and the Eclipse Compiler
Simplest Way to Read Json from a Url in Java