JavaFX:FXML项目为空

时间:2015-12-22 11:49:34

标签: java javafx rmi fxml fxmlloader

当我在服务器添加客户端时尝试更新我的JavaFX应用程序时,在使用更新功能时,我会收到FXML列表视图的空指针。任何线索,为什么它将在我的intitialise和我的事件中工作,但不在我的更新方法

FXMLController

public class FXMLController implements Initializable, Remote, ClientObserver {


    @FXML private ListView listview;    

    List<RMIClient> clients;
    RMIServer server;

    public FXMLController(RMIServer server) throws RemoteException
    {
        this.server = server;
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        if(server != null) {
            try {
            ObservableList<RMIClient> clients = FXCollections.observableArrayList();
            clients.addAll(server.getClients());
            if(listview.getItems() == null)
                this.listview.setItems(clients);
            else
                this.listview.getItems().setAll(clients);
            } catch (RemoteException ex) { }
        }
    }    

    @Override
    public void update() {
       System.out.println(listview);
    }

}

My Mainapp

public class MainApp extends Application {

    public static RMIServer server;
    private FXMLController controller;
    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("/fxml/Scene.fxml"));
        controller = new FXMLController(server);
        loader.setController(controller);
        server.setObserver(controller);
        Parent root = loader.load();

        Scene scene = new Scene(root);
        scene.getStylesheets().add("/styles/Styles.css");

        stage.setTitle("RMINetwork Centre");
        stage.setScene(scene);
        stage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {

         try 
         {
            server = (RMIServer) Naming.lookup("rmi://localhost:1099/" + RMIServer.class.getTypeName());

         } 
         catch(Exception e) { } 
         launch(args);
    }

}

Scene.fxml

<BorderPane prefHeight="350" prefWidth="500" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" >
   <left>
    <ListView fx:id="listview" prefHeight="398.0" prefWidth="152.0" onMouseClicked="#ChangeContext" />
</left>
</BorderPane>

服务器类:

public class Server extends UnicastRemoteObject implements RMIServer {

    private List<RMIClient> clients;
    private ClientObserver observer;


    public Server() throws RemoteException 
    {
        this.clients = new ArrayList<>();
    }

    @Override
    public void registerClient(RMIClient client) throws RemoteException 
    {
        this.clients.add(client);
        if(observer != null) 
        {  
            this.observer.update(); 
        }
    }

    @Override
    public void removeClient(RMIClient client) throws RemoteException 
    {
        this.clients.remove(client);
        if(observer != null) 
        { 
            this.observer.update(); 
        }
    }

        @Override
public String notifyClient(int index, RMICommand cmd) throws RemoteException 
{
    return this.clients.get(index).executeCommand(cmd);
}

@Override
public List<RMIClient> getClients() throws RemoteException
{
    return this.clients;
}

@Override
public void setObserver(ClientObserver observer) throws RemoteException {
    this.observer = observer;
}

有没有人有解决我问题的线索?

亲切的问候

0 个答案:

没有答案