2

Ok So i have made my array and added an action listener so that when the button named "Submit" is clicked all data from my JTextFields should be entered into an ArrayList although this is not happening, any help on why not would be appreciated. below is the Action Listener action Performed.

public class Main {
    String HouseNumber, StreetName, Town, Postcode, Beds, Price, Type;
    JTextField HouseNumber1, StreetName1, Town1, Postcode1, Beds1, Price1,
            Type1;
    JLabel HouseNumberLabel, StreetNameLabel, TownLabel, PostcodeLabel,
            BedsLabel, PriceLabel, TypeLabel;
    JButton Submit;
    JPanel panel;
    JFrame frame;

    public static void main(String[] args) {
        Main gui = new Main();
        gui.go();
    }

    public void go() {
        frame = new JFrame();
        panel = new JPanel();
        HouseNumberLabel = new JLabel("House Number");
        HouseNumber1 = new JTextField("");
        StreetNameLabel = new JLabel("Street name");
        StreetName1 = new JTextField("");
        TownLabel = new JLabel("Town");
        Town1 = new JTextField("");
        PostcodeLabel = new JLabel("Postcode");
        Postcode1 = new JTextField("");
        BedsLabel = new JLabel("Number of beds");
        Beds1 = new JTextField("");
        PriceLabel = new JLabel("Price (£)");
        Price1 = new JTextField("");
        TypeLabel = new JLabel("Building Type");
        Type1 = new JTextField("");
        Submit = new JButton("Submit");
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        frame.getContentPane().add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setVisible(true);

        // Add contents to JFrame and JPanel
        panel.add(HouseNumberLabel);
        panel.add(HouseNumber1);
        panel.add(StreetNameLabel);
        panel.add(StreetName1);
        panel.add(TownLabel);
        panel.add(Town1);
        panel.add(PostcodeLabel);
        panel.add(Postcode1);
        panel.add(BedsLabel);
        panel.add(Beds1);
        panel.add(PriceLabel);
        panel.add(Price1);
        panel.add(TypeLabel);
        panel.add(Type1);
        panel.add(Submit);
        frame.pack();
        frame.show();

        final ArrayList<Main> p = new ArrayList<Main>();
        Submit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Main array = new Main();
                HouseNumber = HouseNumber1.getText();
                StreetName = StreetName1.getText();
                Town = Town1.getText();
                Postcode = Postcode1.getText();
                p.add(array);
            }
        });
    }
}
2
  • 1
    For better help sooner, post an SSCCE. Commented Apr 29, 2011 at 17:56
  • 2
    Using standard Java variable naming conventions. Variable names should NOT start with an upper case character. Commented Apr 29, 2011 at 19:51

1 Answer 1

6

Although your Main class has the fields, since it's also managing the GUI, you don't want to create an ArrayList<Main>

If you just need to collect all the strings then you can create

ArrayList<String> houseDetails = new ArrayList<String>();

houseDetails.add(HouseNumber);
houseDetails.add(StreenName);
houseDetails.add(Town);
houseDetails.add(Postcode);

but the cleaner thing to do would be to create a class to manage these

class House
{
    private String houseNumber;
    private String streetName;
    private String town;
    private String postcode;

    public String getHouseNumber() {
        return houseNumber;
    }
    public void setHouseNumber(String houseNumber) {
        this.houseNumber = houseNumber;
    }
    public String getStreetName() {
        return streetName;
    }
    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }
    public String getTown() {
        return town;
    }
    public void setTown(String town) {
        this.town = town;
    }
    public String getPostcode() {
        return postcode;
    }
    public void setPostcode(String postcode) {
        this.postcode = postcode;
    }
}

and then create a House and set all the valuse.

final ArrayList<House> houses = new ArrayList<House>();

and in your actionPerformed event

House house = new House();
house.setHouseNumber(HouseNumber);
...

houses.add(house);
Sign up to request clarification or add additional context in comments.

8 Comments

What do you mean by settlers?
how does your Main class take these string ? there's got to be these set methods or just fields like array.HouseNumber = HouseNumber;
would it be any better if i could show you my whole code? im a little confused lol
yeah that would help. but if it's too much code (> 150 lines) then why don't you create a pastie from pastie.org and post the link in your question
i have editted the main post showing the all of the code from the class, id be very grateful if you could send me in the right direction so that the data is passed into the array list, Thanks in advance
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.