How to open a GUI built in Eclipse by Netbeans?

3

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

    
asked by anonymous 04.03.2015 / 20:06

1 answer

1

In Netbeans for it to be recognized as a form and work correctly there are several requirements. I'll start with the simplest (but least important). First, the constructor should have the following form:

public MinhaTela() {
    initComponents();
}

You can even change the constructor however you want, but it is important to keep the call to initComponents() . What is the initComponents() format? Typically it looks 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 code of initComponents() and variables declared at the end of the class) will be colored gray by Netbeans. It will not normally let you change them directly by code. If you open in an external editor (example, Notepad ++) you will see more code than 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 the code against modifications. If you remove them with an external tool Netbeans will allow you to directly tweak this code.

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

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

But what if you do not have this file .form ? If you do not have this file, Netbeans will not recognize your JFrame as editable by the GUI builder. In this case, if you do not want to simply forget the edit by the GUI builder and just stay in the code, you can create an empty .form file file to begin rebuilding the entire layout .

If you are going to recreate the entire layout , you should do a backup since Netbeans will automatically change your class from the information contained in the% and also make sure that .form and end-of-class variables are present following the template shown above (possibly including comments initComponents() and GEN-BEGIN ). Here is an empty GEN-END file file to get 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>

In addition, you can learn the structure of the .form file as you manipulate the Netbeans GUI builder when analyzing how the XML changes and how these changes are reflected in Java code, but I will not go in in many details in this because it is already somewhat out of the question.

    
05.03.2015 / 03:35