Java rewritable arrays

Hey, I'm a little new here, so I'm sorry if I messed up something in my post. In any case, the problem I am facing is with arrays, what I'm trying to do basically is to use String [] arrays to fill my form and display it on the screen, and then getForm () returns String [] with the name forms and information in the text [i] . All this works fine until I use the button that I added to call the getForm () function and I go to another form ( createForm () attached to the ListListener) and all the shortcuts are displayed like everything that was returned in the getForm function (). I'm sure this has something to do with how I use my arrays, but I thought that they would return to normal after I selected another list item that goes through the createForm () function, resetting the arrays again, so I'm not sure what is going on.

thank

I have included a screenshot of what I mean too.

http://www.majhost.com/gallery/adc90/afsd/error.png

class Form extends JPanel
{
    //Arrays for the forms
    private String[] com = {"Communication","ICAO","Type","Frequency"};
    private String[] fuel = {"Fuel","ICAO","Type"};
    private String[] runway = {"Runway","ICAO","Number","Type","Length"};
    private String[] airplane = {"Airplane","Make","Model","Type","Fuel Capacity", "Fuel Burn Rate", "Air Speed"};
    private String[] airport = {"Airplane","ICAO","Name","Longitude","Latitude","crFreq","crType", "Fuel Type"};

    //Declare variables
    private JTextField[] text;
    private String[] formReturn;
    private String[] formArray;
    private JButton submit,clear;

    public Form()
    {
        createForm("Airplane");
    }

    public void createForm(String choice)
    {
        removeAll();
        if(choice.equals("Communication"))
        {
            formArray = com;
        }
        else if(choice.equals("Fuel"))
        {
            formArray = fuel;
        }
        else if(choice.equals("Airplane"))
        {
            formArray = airplane;
        }
        else if(choice.equals("Airport"))
        {
            formArray = airport;
        }
        else if(choice.equals("Runway"))
        {
            formArray = runway;
        }


        int l = formArray.length + 1;
        text = new JTextField[l];

        //Layout info
        GridLayout grid = new GridLayout(l,2);
        grid.setHgap(0);
        setLayout(grid);
        //Set label
        add(new JLabel(formArray[0]));
        add(new JLabel(""));
        for(int i = 1; i < formArray.length; ++i)
        {
            add(new JLabel(formArray[i]));
            add(text[i] = new JTextField(20));
        }

        //Add in the buttons and the actionlisteners
        submit = new JButton("Create");
        clear = new JButton("Delete");
        add(clear);
            clear.addActionListener(new Button());
        add(submit);
            submit.addActionListener(new Button());
        updateUI();
    }
    //Get form info
    //This works so far
    public String[] getForm()
    {
        formReturn = formArray;
        formReturn[0] = formArray[0];
        for(int i = 1; i < formReturn.length; i++)
            formReturn[i] = text[i].getText();
        return formReturn;
    }
    //Clear form
    public void clearForm()
    {
        for(int i = 1; i < formArray.length; i++)
            text[i].setText("");
    }
}

+3
source share
1 answer
public String[] getForm()
{
    formReturn = formArray; /* (0) */
    formReturn[0] = formArray[0];
    for(int i = 1; i < formReturn.length; i++)
        formReturn[i] = text[i].getText(); /* (1) */
    return formReturn;
}

Look at line (1): you are changing the formReturn array, which points to the text of the labels. formReturn → formArray → com.

To fix this, just create a new String array in (0):

formReturn = new String[formArray.length];
+4
source

All Articles