Home » Java » java – JavaFX ListView setItems() cannot resolve symbol-Exceptionshub

java – JavaFX ListView setItems() cannot resolve symbol-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

Creating a JavaFX GUI application and can’t get ListView’s to work on my Controller class. Here is the code:

MP3.Java

package mp3player;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Stage;

public class MP3 extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("mp3player.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();

    }


    public static void main(String[] args) {
        launch(args);
    }
}

MP3Controller.java

package mp3player;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.ListView;

public class MP3Controller {
    ObservableList<String> songs = FXCollections.observableArrayList("Song 1", "Song 2");
    ListView<String> songsView = new ListView<String>();

    songsView.setItems(songs);
}

mp3player.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<GridPane alignment="center" hgap="10" scaleY="1.0" vgap="10" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mp3player.MP3Controller">
   <columnConstraints>
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints maxHeight="200.0" minHeight="171.0" prefHeight="200.0" />
      <RowConstraints maxHeight="29.0" minHeight="0.0" prefHeight="0.0" />
   </rowConstraints>
   <children>
      <VBox prefHeight="200.0" prefWidth="100.0">
         <children>
            <HBox prefHeight="50.0" prefWidth="100.0" />
            <HBox prefHeight="100.0" prefWidth="200.0">
               <children>
                  <ListView fx:id="songsView" prefHeight="67.0" prefWidth="146.0" />
               </children></HBox>
            <HBox prefHeight="47.0" prefWidth="100.0" />
         </children>
      </VBox>
   </children>
</GridPane>

Any help would be greatly appreciated. I am unsure as to what’s happening here as I am following the docs pretty closely, I have a suspicion it might be to do with importing javafx? If so, you could try running the script in your machine and seeing if it works. Thanks everyone!

How to&Answers:

try following
in your code you are trying to set values outside method or constructor that why it is not allowing you to set value

you can do by creating constructor or method

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.ListView;

public class MP3Controller {
    ObservableList<String> songs = FXCollections.observableArrayList("Song 1", "Song 2");
    ListView<String> songsView = new ListView<String>();
    MP3Controller() {
        songsView.setItems(songs);
    }
}