填写JavaFX表

时间:2017-10-30 20:12:32

标签: java javafx

我是javaFX的新手,我正在尝试用来自ObservableList clientData的数据填充我的clientDataTable。我的ConnectionsPane构造函数创建一个测试客户端对象,我试图让我的ConnectionsPaneController填充表。我是javaFX的新手,无论我做什么,我似乎无法让表格填充。有人可以帮帮我吗?

我将包含用于此过程的文件。

我遗漏了Client.java类。现在添加。

谢谢!

====

编辑 - 所以我实施了下面要求的更改。我还查看了示例资源并尝试使用它们。尝试在

设置表项时,我一直回到null excecption错误

clientDataTable.setItems(ConnectionsPane.getClientData());

但是当我打印出ConnectionsPane.getClientData()时,它确实给了我一个包含客户端的可观察列表。

编辑2 - 所以我花了更多时间查看示例并尝试将其转换为我的设置,因为我需要这个特定的菜单设置。我也看了重复的帖子,我更新了我的代码,所以它不是静态的。代码现在运行正常,它创建并在控制台中显示的客户端对象,但它仍然不会显示在表中。我认为这可能与控制器访问有关,但不确定。我从没想过桌子会这么麻烦:)有人可以帮忙吗?

MainMenu.java



package io.ironbytes.corkscrew.models;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainMenu extends Application {
    Parent root;
    Stage stage;

    @Override
    public void start(Stage primaryStage) {

        try {
            root = FXMLLoader.load(getClass().getResource("../views/MainMenu.fxml"));
            stage = primaryStage;
            stage.setTitle("Welcome");
     
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    public static void main(String[] args) {
    		launch(args);
    }   
}




ConnectionsPaneController.java



package io.ironbytes.corkscrew.controllers;

import io.ironbytes.corkscrew.models.Client;
import io.ironbytes.corkscrew.models.ConnectionsPane;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

public class ConnectionsPaneController {

	@FXML private TableView<Client> clientDataTable;
	@FXML private TableColumn<Client, String> online;
	@FXML private TableColumn<Client, String> username;
	private ConnectionsPane ConnectionsPane;

	public ConnectionsPaneController() {
		
	}
	
    /**
     * Initializes the controller class. This method is automatically called
     * after the fxml file has been loaded.
     */
    @FXML
    private void initialize() {
       username.setCellValueFactory(cellData -> cellData.getValue().getUsername());

    }
    
    /**
     * Is called by the main application to give a reference back to itself.
     * 
     * @param mainApp
     */
    public void setMainApp(ConnectionsPane ConnectionsPane) {
        this.ConnectionsPane = ConnectionsPane;

     // Prints: [Client [clientID=1, isOnline=true, username=StringProperty [value: test], ipAddress=1.1.1.1]]
        System.out.println(ConnectionsPane.getClientData());
     // Add observable list data to the table
        clientDataTable.setItems(ConnectionsPane.getClientData());
        clientDataTable.refresh();
       
    }
	
}
&#13;
&#13;
&#13;

ConnectionsPane.java

&#13;
&#13;
package io.ironbytes.corkscrew.models;

import java.io.IOException;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.AnchorPane;

public class ConnectionsPane extends AnchorPane{
	
    /**
     * The data as an observable list of Persons.
     */
    public ObservableList<Client> clientData = FXCollections.observableArrayList();
    

    public ConnectionsPane(){
    	
        //Add a test client to the ObservableList
        clientData.add(new Client(1));
        
        System.out.println(getClientData());
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../views/ConnectionsPane.fxml"));
        
        //Give the controller access to the main app.
        //ConnectionsPaneController controller = fxmlLoader.getController();
        //controller.setMainApp(this);
        
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public ObservableList<Client> getClientData() {
        return clientData;
    
    }

}
&#13;
&#13;
&#13;

ConnectionsPane.fxml

&#13;
&#13;
package io.ironbytes.corkscrew.models;

import java.io.IOException;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.AnchorPane;

public class ConnectionsPane extends AnchorPane{
	
    /**
     * The data as an observable list of Persons.
     */
    public ObservableList<Client> clientData = FXCollections.observableArrayList();
    

    public ConnectionsPane(){
    	
        //Add a test client to the ObservableList
        clientData.add(new Client(1));
        
        System.out.println(getClientData());
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../views/ConnectionsPane.fxml"));
        
        //Give the controller access to the main app.
        //ConnectionsPaneController controller = fxmlLoader.getController();
        //controller.setMainApp(this);
        
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public ObservableList<Client> getClientData() {
        return clientData;
    
    }

}
&#13;
&#13;
&#13;

Client.java

&#13;
&#13;
package io.ironbytes.corkscrew.models;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Client {
	
	private int clientID;
	private boolean isOnline;
	private StringProperty username;
	private String ipAddress;
	
	public Client() {

	}
	
	public Client(int ClientID) {
		this.clientID=ClientID;
		this.isOnline=true;
		this.ipAddress="1.1.1.1";
		this.username = new SimpleStringProperty("test");
	}

	@Override
	public String toString() {
		return "Client [clientID=" + clientID + ", isOnline=" + isOnline + ", username=" + username + ", ipAddress="
				+ ipAddress + "]";
	}

	public int getClientID() {
		return clientID;
	}
	
	public void setClientID(int clientID) {
		this.clientID = clientID;
	}

	public boolean isOnline() {
		return isOnline;
	}

	public void setOnline(boolean isOnline) {
		this.isOnline = isOnline;
	}

	public StringProperty getUsername() {
		return username;
	}

	public void setUsername(StringProperty username) {
		this.username = username;
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}


}
&#13;
&#13;
&#13;

0 个答案:

没有答案