JavaFX ListView füllen in Controller Class

fabipfolix

Mitglied
Hallo,
ich habe bereits einige Variationen probiert, jedoch ist es mir nie gelungen den Inhalt einer im Scenebuilder generierten ListView darzustellen.
Code:
package application;

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

public class SampleController {

    @FXML
    private static ListView<String> testListView;
   
   
    public static void createListView() {
        ObservableList<String> obsList = FXCollections.observableArrayList (
                "Test1", "Test2", "Test3");
        testListView.setItems(obsList);
       
    }
       
}

Theoretisch müsste der Fehler an der "Erstellung" der ListView liegen, da es manuell erstellt auf diese Weise ja funktioniert.

Ich hoffe jemand kennt die Lösung und kann mir hierbei helfen!
 
Ich rufe das ganz in der Main Klasse auf.
Dabei bekomme ich dann den Fehler:


java.lang.NullPointerException
at application.SampleController.createListView(SampleController.java:17)
at application.Main.start(Main.java:17)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
 
Da ist auch der Fehler.
Ich frag einfach mal in den Raum: Kennst du die Bedeutung von static?

PS: Ich bezweifle dass der SceneBuilder die ListView als static deklariert hat.
 
Ich frag einfach mal in den Raum: Kennst du die Bedeutung von static?
Mehr oder weniger, static sorgt dafür, dass man es unabhängig von Objekten benutzen kann. Muss allerdings auch zugeben, dass mein Wissen dem bezüglich etwas sporadisch ist.

Wäre dir jedoch sehr dankbar, wenn du mich belehren kannst und möglicherweise mir auch erklären kannst, wie ich mein Problem dann lösen kann.
 
Methoden und/oder Variablen die static sind existieren bereits vor jeglicher Instanzierung eines Objekts.

Der Controller einer FXML Datei (und damit auch ihre Komponenten) wird genau ein mal in der Applikation geladen, nämlich g.d.w. die FXML Datei geladen wird.
Ich nehme an, dass du die o.g. Methode schon aufrufst bevor dein Controller überhaupt geladen wurde. Das verursacht die NullPointerException.

Normalerweise implementieren FXController das Initializable Interface welches eine initialize() Methode zur Verfügung stellt. Dort musst du deiner ListView dan
 
Normalerweise implementieren FXController das Initializable Interface welches eine initialize() Methode zur Verfügung stellt. Dort musst du deiner ListView dan
Darauf bin ich bereits einmal gestoßen, war mir jedoch unsicher was das macht.

Wenn ich dich richtig verstanden habe genügt es dann doch den Code der createListView Methode in initializeable zu kopieren. Hierbei stoße ich jedoch auf eine
javafx.fxml.LoadException:
/C:/Users/fabia/workspace/workspace/ListViewTest/bin/application/Sample.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at application.SampleController.initialize(SampleController.java:24)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
Der Pfad passt hierbei soweit, jedoch eigentlich. Oder wird der src Pfad benötigt?
Oder ist das Problem ein anderes?

Kompletter Code:
Java:
package application;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;

public class SampleController implements Initializable {

    @FXML
    private static ListView<String> testListView;
   
    @FXML private URL location;
   

    @Override
    public void initialize(URL l, ResourceBundle resources) {
        ObservableList<String> obsList = FXCollections.observableArrayList (
                "Test1", "Test2", "Test3");
        testListView.setItems(obsList);
    }
       
}

Schon mal Danke, dass du so geduldig bleibst und höflich antwortest!
 
Das brauchst du nicht.
Vermutete ich bereits, wollte es dann jedoch auch probieren.

In Zeile 24 steht
Java:
testListView.setItems(obsList);
Allerdings verstehe ich nicht, was daran nicht passt.
Muss ich mit einer CellFactory arbeiten?

Main:
Java:
package application;
  
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;




public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
          
          
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
  
    public static void main(String[] args) {
        launch(args);
    }
}
 
Meine FXML ist diese hier, die fxid passt aber eigentlich:
Code:
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
   <center>
      <AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <ListView fx:id="testListView" prefHeight="200.0" prefWidth="200.0" />
         </children>
      </AnchorPane>
   </center>
</BorderPane>

Sonst habe ich nur noch die application.css, welche allerdings leer ist.
 

Neue Themen


Zurück
Oben