3

i am looking to pass values entered in my Textfields into an array list, although i cannot seem to do this. i am able to view the details which have been entered in the console when using

       System.out.println(houses.get(1).getHouseNumber())

but this does not post the newly entered values into the arraylist in my code and i can not understand why.

ArrayList and ActionListener code

final ArrayList<House> houses = new ArrayList<House>();
        houses.add(new House());
 button.addActionListener(new ActionListener()
 {
        public void actionPerformed(ActionEvent e) {
        House house = new House();
        house.setHouseNumber(houseNumber.getText());
        house.setStreetName(streetName.getText());
        house.setTown(town.getText());
        house.setPostcode(postcode.getText());
        houses.add(house);
            System.out.println(houses.get(1).getHouseNumber());
            System.out.println(houses.get(1).getStreetName());
            System.out.println(houses.get(1).getTown());
            System.out.println(houses.get(1).getPostcode());
       }
   });

GUI Code

  public void go(){
    frame = new JFrame();
    panel = new JPanel();
    HouseNumberLabel = new JLabel ("House Number");
    houseNumber = new JTextField ("");
    StreetNameLabel = new JLabel ("Street name");
    streetName = new JTextField ("");
    TownLabel = new JLabel ("Town");
    town = new JTextField ("");
    PostcodeLabel = new JLabel ("Postcode");
    postcode = new JTextField ("");
    BedsLabel = new JLabel ("Number of beds");
    beds = new JTextField ("");
    PriceLabel = new JLabel ("Price (£)");
    price = new JTextField ("");
    TypeLabel = new JLabel ("Building Type");
    type = new JTextField ("");
    button = 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)

House Class code

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;
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Darren Burgess
  • 4,200
  • 6
  • 27
  • 43
  • you're not getting values from `JtextField` according to your code. `Submit` is of what type? – AnaZgombic May 01 '11 at 13:47
  • @Darren: a suggestion, don't use capital letter as a first letter in variable name. And from where you are getting houseNumber,town,...and all that information? – Harry Joy May 01 '11 at 13:49
  • @Harry Joy: Thanks i have changed the variable submit to lower case and i have added additional code showing where houseNUmber is coming from – Darren Burgess May 01 '11 at 13:54
  • You may want to make sure that the event related to your listener is actually being triggered. By knowing what is "submit" perhaps we can infer more. But the bug is not obvious here. – Edwin Dalorzo May 01 '11 at 13:57
  • @edalorzo: ow would i go about making sure the action listener is triggering? and submit is just my jbutton which is supposed to post the details into the arraylist – Darren Burgess May 01 '11 at 14:04
  • @Darren: check for arayList size after every time you add new item in it. In simple word debug your program using some IDE like Eclipse. – Harry Joy May 01 '11 at 14:06
  • arraysize is 1 and does not increase when details should be submitted, hmm – Darren Burgess May 01 '11 at 14:09
  • @Darren: is your `houses` variable global? – Harry Joy May 01 '11 at 14:24
  • houses is a global variable yes – Darren Burgess May 01 '11 at 14:57
  • i'm suspecting some other button is getting the event from the form being displayed and not the one posted in your code above. posting the complete code which generates the GUI would help. – AnaZgombic May 01 '11 at 15:17
  • is it necessary to have "implements actionListener" declared in the class? when i do this i am told to add abstract methods but there should be no abstract methods because i have all of the aciotnlistener components :S – Darren Burgess May 01 '11 at 15:54

2 Answers2

2
String houseNumber;  

This variable is not needed, get rid of it;

JTextField HouseNumber1; 

This does not follow proper java naming conventions. Variable names do not start with an upper cased character. The variaible name should be:

JTextField houseNumber; 

Now in the ActionListener you get the text from the text field directly:

house.setHouseNumber( houseNumber.getText() );   

Edit:

okButton.addActionListener( new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        System.out.println( "ok" );
    }
});
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks this has helped although nothing is posting still, is it necessary to have "implements actionListener" declared in the class? when i do this i am told to add abstract methods but there should be no abstract methods because i have all of the aciotnliistener components :S – Darren Burgess May 01 '11 at 14:57
  • Generally you don't implement the ActionListener interface because you might have more than one button in your class that is listener for ActionEvents. A common solution is to use an annonymous inner class for the ActionListener. See the edit. – camickr May 01 '11 at 16:02
1

used it to print out the array and was presented with [demo2.House@e2eec8, demo2.House@aa9835].

House@e2eec8 and House@aa9835refer to the first and second house in the ArrayList, respectively. Try something like this instead:

System.out.println(houses.get(0).getHouseNumber());

Addendum:

I cannot see the details in the ArrayList in my code.

You can override toString() in House; that way you can just do this:

System.out.println(houses.get(0));

Addendum:

This does not post the newly entered values into the ArrayList.

System.out.println(houses.get(1).getHouseNumber());

Right, this always gets the second entry, starting from 0. The following will always get the last one sent to add().

System.out.println(houses.get(houses.size() - 1).getHouseNumber());

Addendum: You can loop through the houses like this.

for (House h : houses) {
    System.out.print(h.getHouseNumber());
    System.out.print(h.getStreetName());
    ...
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Unless you put the results there some other way. You might update your question to show your latest code. – trashgod May 01 '11 at 19:16
  • just tried this and it works, house number is printed in the console, just to clarify, does this mean that the details have now been passed into my array? just i cannot see the details in the Arraylist in my code – Darren Burgess May 01 '11 at 19:17
  • I've elaborated above. You can just print `house.get…`, but I think you're right to verify that it was added. You can also check the result returned by `houses.add(house)`. – trashgod May 01 '11 at 21:25
  • Although this does show me the last entered details they still do not appear to be saving to the array list, which is what i need them to do, when i try and print the array list to see all of its contents i am still shown [demo2.House@12d03f9] instead of the actual details. should the details appear in my code under the line; "final ArrayList houses = new ArrayList();" as they would if they were entered in manually in code its self? – Darren Burgess May 01 '11 at 22:22
  • The symbol `House@12d03f9` is a reference to the whole `House` instance. You may want to loop through the array to see them all, as shown above. What textbook are you using? – trashgod May 01 '11 at 22:49
  • not using a textbook really its a piece of extra work ive been given from school, been reading Head First Java to get the jist, why do you ask? – Darren Burgess May 01 '11 at 23:00
  • been doing a module on oo at the moment so doing a little extra work because it interests me. – Darren Burgess May 01 '11 at 23:14
  • Then definitely look at overriding `toString()`, as suggested [here](http://stackoverflow.com/questions/5064027/sorting-2d-array-of-string-in-java/5064357#5064357). – trashgod May 01 '11 at 23:23
  • i have just looked at your latest edit regarding looping through the houses and the result is null null null, i assume this means that the values aren't being saved into the arraylist? ps thanks for bearing with me – Darren Burgess May 01 '11 at 23:35
  • Not exactly: it means that three `null` references have been added. If you have a debugger, try stepping through the code to see where it's gone awry. – trashgod May 01 '11 at 23:55