0

I got items on my listview roles but they are not showing in my program after running in window. Gui done in Scene Builder. When i run program field where should be items is white blank just empty.

printscreen of program https://zapodaj.net/c60f99a10f300.jpg.html

scene builder https://zapodaj.net/26fbf2123397c.jpg.html

When i use show sample data in Scene Builder it shows lorem ipsum etc.

here u got code from controller and fmxl document.

package sample;

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

import java.net.URL;
import java.util.*;

public class ControllerBindColors implements Initializable {
    @FXML
    private ListView<String > roles;
    @FXML
    private ListView<String> colors;
    @FXML
    private ListView<?> binded;
    String selectedRole;
    String selectedColor;
    int numberOfPlayers;
    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    public void initData(int numberOfPlayers, String selectedRole, String selectedColor) {
        this.numberOfPlayers = numberOfPlayers;
        this.selectedRole = selectedRole;
        this.selectedColor = selectedColor;

        ObservableList<String> itemsRoles = FXCollections.observableArrayList("test","test2","test3");
        roles = new ListView<>();
        roles.setItems(itemsRoles);
        roles.getItems().addAll("test555");
        roles.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        System.out.println(roles.getItems());
    }

}

fxml

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>


<VBox spacing="10.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ControllerBindColors">
   <children>
      <HBox alignment="CENTER">
         <children>
            <ListView fx:id="roles" prefHeight="200.0" prefWidth="200.0" />
            <VBox alignment="CENTER" spacing="10.0">
               <children>
                  <Button fx:id="backBtn" ellipsisString="BACK" mnemonicParsing="false" onAction="#back" text="BACK" />
                  <Label prefHeight="51.0" prefWidth="71.0" text="Select role and color then bind." textAlignment="CENTER" wrapText="true" />
                  <Button fx:id="bindbtn" ellipsisString="BIND" mnemonicParsing="false" onAction="#bind" text="BIND" />
                  <Button fx:id="nextBtn" ellipsisString="NEXT" mnemonicParsing="false" onAction="#next" text="NEXT" />
               </children>
               <opaqueInsets>
                  <Insets />
               </opaqueInsets>
            </VBox>
            <ListView fx:id="colors" prefHeight="200.0" prefWidth="200.0" />
         </children>
      </HBox>
      <ListView fx:id="binded" prefHeight="200.0" prefWidth="200.0" />
   </children>
   <padding>
      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
   </padding>
</VBox>

initData ControllerNewGame.java startGame() is called when is activaded button

@FXML
    void startGame() {
        try{
            Parent root= FXMLLoader.load(getClass().getResource("BindColor.fxml"));

            ControllerBindColors controller = new ControllerBindColors();
            int number = (int)slider.getValue();
            controller.initData(number,selectedRole.getText(),selectedColor.getText());
            Stage toGame = (Stage)startButton.getScene().getWindow();
            toGame.setTitle("Parasite Pip-Boy Bind Color Menu");
            toGame.setScene(new Scene(root, 600, 300));
            toGame.show();
        }catch(IOException e) {
            startButton.setText("Reinstall program. BindColor.fxml is missing or is corrupted. :(");
        }
    }

list has items for sure in console there are printed all 4 items [test, test2, test3, test555]

  • 1
    where do you call initData? – kleopatra Feb 03 '19 at 16:45
  • in the other controller of window before switching to bindcolors controller.initData(number,selectedRole.getText(),selectedColor.getText()); – Grzegorian Feb 03 '19 at 16:48
  • See [Accessing FXML controller class](https://stackoverflow.com/questions/10751271/) and [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963). You're calling `initData` on a newly created controller instance that is not linked with the loaded FXML. – Slaw Feb 03 '19 at 21:46

1 Answers1

0

To get a reference to the controller used by the FXML (instead of constructing a new one):

FXMLLoader loader = new FXMLLoader();
Parent root= loader.load(getClass().getResource("BindColor.fxml").openStream());
ControllerBindColors controller = fxmlLoader.getController(); 

after getting a reference to it, you can use it as you did:

controller.initData(number,selectedRole.getText(),selectedColor.getText());
c0der
  • 18,467
  • 6
  • 33
  • 65
  • You have an extra equals here `ControllerBindColors controller = = ...` unrelated you don't need to cast the `fxmlLoader.getController();` but whatever helps – Matt Feb 04 '19 at 20:56