因此,我遇到的主要问题是尝试设置columnUUID.setCellValueFactory( new PropertyValueFactory<Person,String>("different") );时,它将填充columnPlayer.setCellValueFactory( new PropertyValueFactory<Person,String>("player") );的工作原理。如果我都将值都设置为“ player”,则会使用这些值填充两个表。如果两个方法都设置为“ different”,则只会添加空白文本。在两个类中将“ different”更改为不同的值不会执行任何操作。

package com.sandislandserv.rourke750.Gui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Map;

import com.sandislandserv.rourke750.BetterAssociations;
import com.sandislandserv.rourke750.database.BaseValues;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;


公共类GuiController {

@FXML private TextField username;
@FXML private TextField domain;
@FXML private PasswordField password;
@FXML private AnchorPane pane;
@FXML private Button login;
@FXML private TableView<Person> listofPlayers;
@FXML private Label badLogin;
@FXML private TableColumn<Person, String> columnUUID;
@FXML private TableColumn<Person, String> columnPlayer;

private ObservableList<Person> data = FXCollections.observableArrayList();
public GuiController() {
}

public AnchorPane getPane(){
    return pane;
}

@FXML
public void Login(){
    data.clear();
    badLogin.setVisible(false);
    System.out.print(username.getText());
    System.out.print(password.getText());
    String players = getPlayers(username.getText().toString(), password.getText().toString());
    String[] player = players.split(" ");
    for (int x = 0; x < player.length; x=x+2){
        System.out.println(player[x] + " " + player[x+1]); // makes sure that both values are populated [for debugging]
        data.add(new Person(player[x], player[x+1]));
    }
    columnUUID.setCellValueFactory(
            new PropertyValueFactory<Person,String>("different")
        );
    columnPlayer.setCellValueFactory(
            new PropertyValueFactory<Person,String>("player")
        );
    listofPlayers.getItems().setAll(data);
}

public String getPlayers(String username, String password){
    StringBuilder builder = new StringBuilder();
    try {
        @SuppressWarnings("resource")
        Socket socket = new Socket(domain.getText().toString(), 25549);
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out.println(username + " " + password);
        if (input.readLine().equals("BAD")) {
            badLogin.setVisible(true);
            return null;
        }
        out.println("Get All Player Info");
        String line;
        while ((line = input.readLine()) != null){
            if (line.equals("END")) break;
            builder.append(line + " ");
        }
        out.println("END");
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return builder.toString();
}


}
`

`
包com.sandislandserv.rourke750.Gui;

导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;

公共类人{

private StringProperty different;
private StringProperty player;

public Person(String player, String playerid) {
    this.player = new SimpleStringProperty(player);
    different = new SimpleStringProperty(playerid);
}

public String getPlayer(){
    return player.get();
}

public String getUUID(){
    return different.get();
}


}

`

最佳答案

将您的getUUID方法重命名为getDifferent,它应该可以工作。更好的是,提供一个返回differentPropertydifferent方法。

PropertyValueFactory根据您提供的名称寻找一种方法。 javadoc中的更多信息。

09-27 09:40