How to open a GUI built in Eclipse by Netbeans?

Asked

Viewed 790 times

3

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

  • You made it code using drag and drop (drag and drop tool) from the netbeans itself or made "the hand"?

  • I did the eclipse drag and drop

  • Tried to use the option to import eclipse code?

  • no longer has the eclipse only the dry code.

1 answer

1


In Netbeans for it to be recognized as a form and function properly 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(). How the shape of initComponents()? Typically it is so:

/**
 * 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 the initComponents() and the variables declared at the end of the class) will be grayed out by Netbeans. He usually won’t let you change them directly through the code. If you open in an external editor (example, 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

Look at these //GEN-BEGIN:initComponents, //GEN-END:initComponents, //GEN-BEGIN:variables and //GEN-END:variables? They are delimiters that Netbeans uses to protect code against modifications. If you remove them with an external tool the Netbeans IDE will allow you to move this code directly.

Like 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 decides to change it. The editor’s form settings are inside a file with the extension .form in the same folder as your class. So, if your file contains your subclass of JFrame is called MinhaTela.java, there should be a file MinhaTela.form in the same folder.

It’s in this file .form that the screen editor details are stored (in an XML format). So in order for Netbeans to recognize your screen in its editor, this file must be present.

But what if you don’t have this file .form? 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 constructor and stay in code only, you can create a file .form empty to start recreating the layout whole.

If you’re going to recreate the layout whole, should make a backup since Netbeans will automatically change its class from the information contained in the file .form and also make sure that the initComponents() and class end variables are present following the model shown above (possibly including comments GEN-BEGIN and GEN-END). Here is a file .form empty to begin with:

<?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 file structure is .form as you manipulate the Netbeans GUI builder when analyzing how XML is changed and how these changes are reflected in Java code, but I won’t go into too much detail about it as it is already somewhat outside the purpose of the question.

  • very precise answer. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.