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>