Observable List
Asked Answered
C

2

5

when trying to declare a new ObservableList:

ObservableList<Account> userAccounts = new FXCollections.observableArrayList();

I am getting an error at observableArrayList(); which says:

cannot find symbol, symbol: class observableArrayList, location: class FXCollections.

Here are my import statements

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

And here is my method

public ObservableList<Account> getUsersAccounts(int memberID) { 
    ObservableList<Account> userAccounts = new FXCollections.observableArrayList();

    try {     
        Statement stmt = conn.createStatement();            
        String sql = "SELECT * FROM account WHERE member_id='" + memberID + "'";            
        ResultSet rs = stmt.executeQuery(sql);

        while(rs.next()) {
            Account account = new Account(rs.getInt("member_id"), rs.getString("account_type"), rs.getDouble("balance"));
            userAccounts.add(account);
        }
    } catch (SQLException ex) {
        Logger.getLogger(JDBCManager.class.getName()).log(Level.SEVERE, null, ex);
    }

    return userAccounts;
}

What am I missing, why can't I declare a new ObservableList?

Convertiplane answered 30/1, 2018 at 21:8 Comment(7)
Get rid of the new.Goblin
observableArrayList() is a static method, not a class.Discophile
that worked, but why? If i wanted to make an array list for example, ArrayList<String> test = new ArrayList();, I would need the newConvertiplane
you don need to use new for a static methods so use: ObservableList<Account> userAccounts = FXCollections.observableArrayList();Naphthol
The new keyword is used when you call a constructor.Discophile
For a general discussion, read: What are static factory methods?Socratic
@fr33zex, you've been provided 2 nice answers. Find a more useful one for you and accept it! What should I do when someone answers my question?Reiter
R
7

An instance can be created directly by using a constructor or implicitly by calling a method where this constructor can be invoked.

In your case, it's a static method. Have a look at these techniques:

List<String> a = new ArrayList<>();
List<String> b = Lists.createList();

class Lists {
    public static <T> List<T> createList() {
        return new ArrayList<>();
    }
}
Reiter answered 30/1, 2018 at 21:23 Comment(1)
I like this answer. This answer addresses the thing that the OP doesn't understand.Bartle
M
7

change

ObservableList<Account> userAccounts = new FXCollections.observableArrayList();

to

ObservableList<Account> userAccounts = FXCollections.observableArrayList();
Meath answered 30/1, 2018 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.