0

I am working with JavaFX and i am trying to add to an ArrayList class from the controller file so when the create button is pressed on the FXML file the controller adds a person object to a class of contacts which is an arrayList however i want the ArrayList class Contacts to be global so i could edit whatever is in or has been added to the list later on

Person class:

package project1;

/**
 *
 * @author Fomnus
 */
public class Person {
    private String firstName;
    private String lastName;

    /**
     * Constructor with parameter
     *
     * @param firstName
     * @param lastName
     */
    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName= lastName;
    }

    /**
     * Method to get the name of person
     */
    public String getFirstName() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }

    /**
     * Method to set the name of person
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

Contacts class ArrayList:

package project1;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Fomnus
 */
public class Contacts {

    public static void main(String args[]) {
      // create an array list
      ArrayList contactsList = new ArrayList();

      Person person1= new Person("John", "Doe");
      Person person2= new Person("Jane", "Rivers");
      Person person3= new Person("Steve", "Smith");
      Person person4= new Person("Lucy", "White");

      contactsList.add(person1);
      contactsList.add(person2);
      contactsList.add(person3);
      contactsList.add(person4);

   }
}
Fomnus
  • 85
  • 1
  • 2
  • 7
  • is there any error you are getting? – jack jay Jan 16 '17 at 20:26
  • Did you try to make your list accessible : `public static List list` ? – Bo Halim Jan 16 '17 at 20:27
  • How would i change the code to make the list accessible? – Fomnus Jan 16 '17 at 20:32
  • 1
    Don't make fields public. Don't make anything static simply to provide access to it. `static` controls the *scope*, not the *visibility*. – James_D Jan 16 '17 at 20:38
  • Your `Contacts` class has no state at all. It just has a `main` method with local variables: those variables are created on the stack when `main` is invoked and are removed when `main` completes. Start by making `contactList` a field and provide (at least) a `get` method for it. Initialize it in the constructor. The structure should be basically the same as your `Person` class - just because you are using a list it shouldn't make any difference to the overall structure. Then you can instantiate `Contacts` and share an instance among your controller(s). – James_D Jan 16 '17 at 20:42
  • In standard UI development jargon, your `Contacts` class is called a *model*. So you are really asking how you share a model among your controllers so that it can be modified (and probably observed) from multiple places. Have a look at http://stackoverflow.com/questions/32342864/applying-mvc-with-javafx (which is more complex, but might give you the idea). – James_D Jan 16 '17 at 20:45
  • @James_D How to access a method of a class without resorting to an object nor a static keyword then? – Bo Halim Jan 16 '17 at 20:46
  • @BoHalim What do you mean by "without resorting to an object"? – James_D Jan 16 '17 at 20:47
  • example : `ClassName classObject = new ClassName();` , Sorry if my English is a little rusty – Bo Halim Jan 16 '17 at 20:48
  • @BoHalim What's the problem with creating an object? – James_D Jan 16 '17 at 20:48
  • You said : *Don't make fields public. Don't make anything static* ,then how to access an element (field, method...) in a class ? – Bo Halim Jan 16 '17 at 20:51
  • @BoHalim You access the data via the reference to the object. `myContactsInstance.getContactsList()`. Not sure what you are asking – James_D Jan 16 '17 at 20:52
  • Your Main class "Contacts" looks like a Java Main instead of a JavaFx Main class. – SedJ601 Jan 17 '17 at 14:30

0 Answers0