Overwriting pre-defined array

荒凉一梦 提交于 2019-12-25 06:01:22

问题


I have an array named columnsArray[] it is pre-defined to contain 6 Strings. When i run my method columns() it should overwrite the columnsArray[] with a new array of Strings that the user selects by checking boxes. The way i have tried to implement this adds each box checked to an arrayList and then convert the arrayList to array[]. However when the code is run, columnsArray is not overwritten.

Here is my code so far:

public class EditView {
    private JFrame frame;
    JCheckBox appNo, name, program, date, pName, country, fileLoc, email, uni,
            countryUni, degree, classification, funding, supervisor,
            rejectedBy, misc;
    public ArrayList<String> columnArrLst;
    public String[] columnsArray = { "Application Number", "Name", "Program",
            "Date", "Project Name", "Country of Uni" };

    public EditView() {

    }

    public void makeFrame() {
        frame = new JFrame("Edit View");
        frame.setPreferredSize(new Dimension(300, 350));
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(0, 2, 20, 20));

        appNo = new JCheckBox("appNo");
        name = new JCheckBox("Name");
        program = new JCheckBox("Program");
        date = new JCheckBox("Date");
        pName = new JCheckBox("Project Title");
        country = new JCheckBox("Country of Origin");
        fileLoc = new JCheckBox("Current File Location");
        // countryRef = new JCheckBox("");
        email = new JCheckBox("Email address");
        uni = new JCheckBox("Last University");
        countryUni = new JCheckBox("Country of last Uni");
        degree = new JCheckBox("Degree");
        classification = new JCheckBox("Degree Classification");
        funding = new JCheckBox("funding");
        supervisor = new JCheckBox("Supervisor");
        rejectedBy = new JCheckBox("Rejected By");
        misc = new JCheckBox("Miscelaneous");
        contentPane.add(appNo);
        contentPane.add(name);
        contentPane.add(program);
        contentPane.add(date);
        contentPane.add(pName);
        contentPane.add(country);
        contentPane.add(fileLoc);
        contentPane.add(email);
        contentPane.add(uni);
        contentPane.add(countryUni);
        contentPane.add(degree);
        contentPane.add(classification);
        contentPane.add(supervisor);
        contentPane.add(rejectedBy);
        contentPane.add(misc);

        JButton changeView = new JButton("Change View");

        changeView.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                columns();
                frame.dispose();
            }
        });

        contentPane.add(changeView);
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }

    public String[] columns() {
        columnArrLst = new ArrayList<String>();
        if (appNo.isSelected()) {
            columnArrLst.add("AppNo");
        }
        if (name.isSelected()) {
            columnArrLst.add("Name");
        }
        if (date.isSelected()) {
            columnArrLst.add("Date");
        }
        if (fileLoc.isSelected()) {
            columnArrLst.add("file Location");
        }
        if (country.isSelected()) {
            columnArrLst.add("Country");
        }
        if (email.isSelected()) {
            columnArrLst.add("Email");
        }
        if (uni.isSelected()) {
            columnArrLst.add("University");
        }
        if (countryUni.isSelected()) {
            columnArrLst.add("Country of Uni");
        }
        if (degree.isSelected()) {
            columnArrLst.add("Degree");
        }
        if (classification.isSelected()) {
            columnArrLst.add("Degree Classification");
        }
        if (pName.isSelected()) {
            columnArrLst.add("ProjectName");
        }
        if (funding.isSelected()) {
            columnArrLst.add("Funding");
        }
        if (supervisor.isSelected()) {
            columnArrLst.add("Supervisor");
        }
        if (rejectedBy.isSelected()) {
            columnArrLst.add("rejected By");
        }
        if (misc.isSelected()) {
            columnArrLst.add("Miscelaneous");
        }
        columnsArray = new String[columnArrLst.size()];
        columnArrLst.toArray(columnsArray);
        return columnsArray;
    }
}

Any ideas why it isn't overwriting? Thanks for any help.


回答1:


Try this one

columnsArray =  columnArrLst.toArray(new String[columnArrLst.size()]);

Hope it helps.




回答2:


replace 2nd last line [columnArrLst.toArray(columnsArray);] with following.

columnsArray = columnArrLst.toArray(columnsArray);




回答3:


There is nothing wrong with your code, array contents are actually changing when you use it as

EditView e = new EditView();
e.makeFrame();

and insert a print loop after

columnArrLst.toArray(columnsArray);

Note that your JFrame is displayed in a different Thread. If you want to check the values, you need to explicitly wait until the button is pressed to see them changed. If you are doing something like:

EditView e = new EditView();
e.makeFrame();
for (String s : e.columnsArray) { System.out.println(s);}

This will print the old values, since the printing thread is actually a different one and prints the values immediately.



来源:https://stackoverflow.com/questions/15547320/overwriting-pre-defined-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!