0

I'm new to Java and Java FX and Scene Builder. I put a TabPane into a BorderPane and am now trying to populate the TableView I put into the TabPane with values. I have 2 classes, the Player Class and the PlayerList Class. will just populate items from somewhere later, like this:

public class PlayerList {

    ObservableList<Player> playerList;

    public PlayerList() {
        getPlayersFromServer();
    }

    private getPlayersFromServer() {
        this.playerList = FXCollections.observableArrayList(
        new Player("freakyy85","Owner");
        new Player("Ky3ak","Owner");
        );
    }
}

Now, how do I populate the TableView rows using something like this:

nickColumn.setCellValueFactory(
    new PropertyValueFactory<Player,String>("nick")
);
groupColumn.setCellValueFactory(
    new PropertyValueFactory<Player,String>("group")
);

I first wondered how to do that and where to do that using scene builder because, i didnt have an fx:id set. Now I have one set, but I simply don't know how to do it.

May Player Class looks like this:

public class Player {
    private SimpleStringProperty nick;
    private SimpleStringProperty group;

    public Player (String nick, String group) {
        this.nick = new SimpleStringProperty(nick);
        this.group = new SimpleStringProperty(group);
    }

    public String getNick () {
        return nick.get();
    }

    public String getGroup () {
        return group.get();
    }

    public void setNick (String nick) {
        this.nick.set(nick);
    }

    public void setGroup (String group) {
        this.group.set(group);
    }
}

FXMLDocumentController.java:

package uctool;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.input.InputMethodEvent;

public class FXMLDocumentController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TableColumn<?, ?> nickColumn;

    @FXML
    private TableColumn<?, ?> groupColumn;

    @FXML
    void handleTextChanged(InputMethodEvent event) {

    }

    @FXML
    void initialize() {
        assert nickColumn != null : "fx:id=\"nickColumn\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
        assert groupColumn != null : "fx:id=\"groupColumn\" was not injected: check your FXML file 'FXMLDocument.fxml'.";

    }
}

FXMLDocument.fxml:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="@../../resources/UCTool.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="uctool.FXMLDocumentController">
   <center>
      <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
        <tabs>
          <Tab closable="false" text="Players">
               <content>
                  <TableView editable="true" onInputMethodTextChanged="#handleTextChanged" prefHeight="200.0" prefWidth="200.0">
                    <columns>
                      <TableColumn fx:id="nickColumn" prefWidth="75.0" text="Nickname" />
                      <TableColumn fx:id="groupColumn" prefWidth="75.0" text="Group" />
                    </columns>
                     <padding>
                        <Insets bottom="3.0" left="5.0" right="5.0" top="3.0" />
                     </padding>
                  </TableView>
               </content>
            </Tab>
          <Tab closable="false" text="Untitled Tab 2" />
        </tabs>
      </TabPane>
   </center>
</BorderPane>
  • Not really sure if you are asking where to put the calls to `setCellValueFactory(...)` or if you are asking something else...? – James_D Mar 02 '17 at 00:24

1 Answers1

0

You need to change the types that Scene Builder provides for the table columns (it has no way to determine the type of the table and the columns):

@FXML
private TableColumn<Player, String> nickColumn;

@FXML
private TableColumn<Player, String> groupColumn;

Then just place the calls to setCellValueFactory(...), as you have them, in the initialize() method. So you end up with

package uctool;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.input.InputMethodEvent;

public class FXMLDocumentController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TableColumn<Player, String> nickColumn;

    @FXML
    private TableColumn<Player, String> groupColumn;

    @FXML
    void handleTextChanged(InputMethodEvent event) {

    }

    @FXML
    void initialize() {
        assert nickColumn != null : "fx:id=\"nickColumn\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
        assert groupColumn != null : "fx:id=\"groupColumn\" was not injected: check your FXML file 'FXMLDocument.fxml'.";

        nickColumn.setCellValueFactory(
            new PropertyValueFactory<Player,String>("nick")
        );
        groupColumn.setCellValueFactory(
            new PropertyValueFactory<Player,String>("group")
        );
    }
}
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Do I just insert the ? Or do I set it using the SceneBuilder in some way? – Uwe Pfeifer Mar 02 '17 at 04:55
  • And, I wonder how the program knows, what to use in nickColumn.setCellValueFactory(new PropertyVluaeFactory("nick")); as im using PlayerList playerList; where the data is stored right now? – Uwe Pfeifer Mar 02 '17 at 04:57
  • I don't know reflections. Or do I have to put this: private void getPlayersFromServer() { this.playerList = FXCollections.observableArrayList( new Player("freakyy85","Owner"), new Player("Ky3ak","Owner") ); // which is in PlayerList.class above the other code? Because right now it's not working.- the table is not populated. i fixed the above code though it compiles. – Uwe Pfeifer Mar 02 '17 at 05:17
  • What I have now exactly is this: http://paste.ubuntu.com/24094458/ But data is not populated. – Uwe Pfeifer Mar 02 '17 at 05:34
  • Ok I set up git:. https://gitlab.com/freakyy85/UCOne/tree/master/src/main/java/de/freakyonline/ucone my current problem is this: http://paste.ubuntu.com/24094574/ can someone help me? :) – Uwe Pfeifer Mar 02 '17 at 06:23
  • Ok, I got it to work. Now, I still have hte problem from above - table is not populated. Source at https://gitlab.com/freakyy85/UCOne/tree/master/src/main/java/de/freakyonline/ucone – Uwe Pfeifer Mar 02 '17 at 06:42