Nadav Vahav
Nadav Vahav

Reputation: 51

JAVAFX - unable to add items to listview

I have a Tab called chat. Inside this Tab I have a list of users as ListView<string>. The content of this ListView was loaded from FXML file using:

FXMLLoader.load(getClass().getResource("chat.fxml"))

I am trying to add items to my listview using java, but when I open the GUI, chat tab, the items I add does not get displayed in the users listview.

One of the ways I tried was:

List<String> values = Arrays.asList("one", "two", "three");
listOfUsers.setItems(FXCollections.observableList(values));

I have tried other ways as well, but nothing seems to work. I'd appreciate your help.

Upvotes: 1

Views: 6563

Answers (3)

Nadav Vahav
Nadav Vahav

Reputation: 51

thanks guys..
the problem was that my listView of users was created by the FXML loader, and then by mistake i initialized it again in the java code, so all was needed is deleting the row:

listOfUsers=new ListView<String>();

Upvotes: 4

Perco
Perco

Reputation: 160

the file chat.fxml has fx:controller?!

You need...

xmlns:fx="http://javafx.com/fxml" fx:controller="DIRECTORY.FILE" //FILE.java

Upvotes: 0

user2174084
user2174084

Reputation:

Try this

BorderPane pane = null;
    try {
        pane = (BorderPane) FXMLLoader.load(HERENAMECLASS.class.getResource("chat.fxml"));
        pane.getStyleClass().add("main");
    } catch (IOException ex) {

        Logger.getLogger(HERENAMECLASS.class.getName()).log(Level.SEVERE, null, ex);
        System.exit(-1);
    }

Upvotes: -2

Related Questions