java – How to open a GUI built in Eclipse by Netbeans?

Question:

I have a code using JFrame , however when I open it through Netbeans I can only change the layout through the code. How do I use the code and edit the layout by Swing's GUI Builder.

Answer:

In Netbeans for it to be recognized as a form and to function correctly there are several requirements. I'll start with the simplest (but least important). First, the builder should look like this:

public MinhaTela() {
    initComponents();
}

You can even change the constructor however you like, but it's important to keep calling initComponents() . How is the initComponents() format? Typically it goes like this:

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {
    // ... Um monte de código ...
}// </editor-fold>  

// Mais um monte de código...

// Variables declaration - do not modify   
// ... Algumas variáveis aqui ...
// End of variables declaration  

These two areas above (the initComponents() code and the variables declared at the end of the class) will be grayed out by Netbeans. It normally won't let you change them directly from the code. If you open it in an external editor (eg Notepad++) you will see more code that Netbeans never shows:

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
    // ... Um monte de código ...
}// </editor-fold>//GEN-END:initComponents

// Mais um monte de código...

// Variables declaration - do not modify//GEN-BEGIN:variables
// ... Algumas variáveis aqui ...
// End of variables declaration//GEN-END:variables

See these //GEN-BEGIN:initComponents , //GEN-END:initComponents , //GEN-BEGIN:variables and //GEN-END:variables ? They are delimiters that Netbeans uses to protect code from modification. If you remove them with an external tool Netbeans will allow you to work directly on this code.

As the initComponents() comment says, the body of this method is automatically generated and regenerated (as well as the variables at the end) whenever the GUI builder decides to change it. The editor's form settings are inside a file with a .form extension in the same folder as your class. So, if your file containing your JFrame subclass is called MinhaTela.java , there must be a MinhaTela.form file in the same folder.

This .form file .form where the screen editor details are stored (in an XML format). So for Netbeans to recognize your screen in its editor, this file must be present.

But what if you don't have this .form file? If you don't have this file, Netbeans will not recognize your JFrame as editable by the GUI builder. In this case, if you don't want to just forget about editing by the GUI builder and just stick to the code, you can create an empty .form file to start recreating the entire layout .

If you are going to recreate the entire layout , you might want to do a backup as Netbeans will automatically change your class from the information contained in the .form file and also make sure that the initComponents() and end of class variables are present following the template shown above (possibly including the GEN-BEGIN and GEN-END comments). Here's an empty .form file to get you started:

<?xml version="1.0" encoding="UTF-8" ?>

<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
  <Properties>
    <Property name="defaultCloseOperation" type="int" value="3"/>
  </Properties>
  <SyntheticProperties>
    <SyntheticProperty name="formSizePolicy" type="int" value="1"/>
    <SyntheticProperty name="generateCenter" type="boolean" value="false"/>
  </SyntheticProperties>
  <AuxValues>
    <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
    <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
    <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
    <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
    <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
  </AuxValues>

  <Layout>
    <DimensionLayout dim="0">
      <Group type="103" groupAlignment="0" attributes="0">
          <EmptySpace min="0" pref="400" max="32767" attributes="0"/>
      </Group>
    </DimensionLayout>
    <DimensionLayout dim="1">
      <Group type="103" groupAlignment="0" attributes="0">
          <EmptySpace min="0" pref="300" max="32767" attributes="0"/>
      </Group>
    </DimensionLayout>
  </Layout>
</Form>

Also, you can learn what the structure of the .form file is as you manipulate the Netbeans GUI builder by analyzing how XML is changed and how these changes are reflected in Java code, but I won't go into too much detail on that. for it is already somewhat out of the question.

Scroll to Top