Netbeans Gui Editor Generating Its Own Incomprehensible Code

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 the Diff 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:

  1. Drop a new component onto the design
    editor 'canvas' for the window.

  2. 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 or DesignGridLayout, which derive some power from accepting human-readable text parameters. I see GroupLayout 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



Leave a reply



Submit