Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Arrays being overwritten

Hey I'm a little new here so my apologies if I messed something up in my post. Anyways the problem I'm having has to deal with arrays, what I'm trying to do basically is use the String[] arrays to populate my form and display it on screen and then have the getForm() function return a String[] with the title of the form and the info in text[i]. This all works fine until I use use the button I added to call the getForm() function and I change to a different form (createForm() attached to ListListener) and all the labels appear as whatever was returned in the getForm() function. I'm pretty sure it has something to do with the way I'm using my arrays but I thought they would be set back to normal after I chose another list item which goes through the createForm() function again resetting the arrays, so I'm not sure whats going on.

Thanks

I've included a screenshot of what I'm referring too bellow as well.

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("");
    }
}

like image 491
adc90 Avatar asked Dec 11 '25 12:12

adc90


1 Answers

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 modify formReturn array which points to labels text. formReturn -> formArray -> com.

To fix it just create new String array at (0):

formReturn = new String[formArray.length];
like image 184
KrHubert Avatar answered Dec 14 '25 01:12

KrHubert