I have some problem with a part of my application that is not working correctly as i want, i have a java fx listview that is like a log, i'm trying to create a simple log with it. look at the code,

I tried to add a Task that write a log with "writeLog" function every seconds and it worked ( the listview refresh correctly with the new string). The second code is the same but without the Task and it doesn't work like i want,in this case writeLog will be called by other classes. When calling writeLog("any string..") from other classes the Observable list will change correctly and the file ( log ) will change as well, but the listview doesn't want to show the new String added to log.

public class ControllerServer implements Initializable {

    @FXML
    public ListView myListView;

    private ObservableList<String> logList = FXCollections.observableArrayList();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        try {
            fillLogList();
            MyTask task = new MyTask();
            Timer timer = new Timer(true);
            timer.scheduleAtFixedRate(task,4000,4000);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (logList!= null) {
            for (int i = 0; i < logList.size(); i++) {
                System.out.println(logList.get(i));
            }
         logList.addListener((javafx.beans.Observable observable)->{
             System.out.println("changed list");
         });
        }
        myListView.setItems(logList);
    }
    //METHODS
    public void fillLogList() throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("samplefile1.txt"));
        String line;
        while((line = reader.readLine())!=null) {
            logList.add(line);
        }
    }
    public void writeLog(String log) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter("samplefile1.txt", true));
        writer.write(log);
        if(logList!=null){
            logList.add(log);
            System.out.println("i'm writing this log inside the file, log say : " + log);
        }else
        writer.newLine();
        writer.close();
    }

    class MyTask extends TimerTask {
        @Override
        public void run(){
            try {
                writeLog("Hello, i'm working inside this task, why?");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
public class ControllerServer implements Initializable {

    @FXML
    public ListView myListView;

    private ObservableList<String> logList = FXCollections.observableArrayList();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        try {
            fillLogList();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (logList!= null) {
            for (int i = 0; i < logList.size(); i++) {
                System.out.println(logList.get(i));
            }
         logList.addListener((javafx.beans.Observable observable)->{
             System.out.println("changed list");
         });
        }
        myListView.setItems(logList);
    }
    //METHODS
    public void fillLogList() throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("samplefile1.txt"));
        String line;
        while((line = reader.readLine())!=null) {
            logList.add(line);
        }
    }
    public void writeLog(String log) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter("samplefile1.txt", true));
        writer.write(log);
        if(logList!=null){
            logList.add(log);
            System.out.println("i'm writing this log inside the file, log say : " + log);
        }else
        writer.newLine();
        writer.close();
    }

}
public class Server extends Application {
    private static ServerLog serverLog = new ServerLog();
    private static ControllerServer  controllerServer = new ControllerServer();


    public static void main(String[] args) throws IOException {
        Runnable r = () -> {
            new File("users").mkdir();
            try {
                ServerSocket socket = new ServerSocket(8189);

                while(true) {
                    Socket s = socket.accept();
                    ServiceHandler client = new  ServiceHandler(s);
                }

            }catch(IOException e){e.printStackTrace();}
        };
        new Thread(r).start();
        controllerServer.writeLog("server starting..");
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("View/server.fxml"));
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.setTitle("Server Log");
        primaryStage.show();
    }

}

package server;

import server.Controller.ControllerServer;
import server.Model.*;

import java.io.*;
import java.net.Socket;

import static common.ServiceID.*;
import static common.ServiceID.FAILURE_RESPONSE;

public class ServiceHandler extends Thread{

    private static Object lock = new Object();
    private static ServerLog serverLog = new ServerLog();
    private static ControllerServer controllerServer = new ControllerServer();
    private Socket s;

    public ServiceHandler(Socket s){
        this.s = s;

        this.start();
    }

    public void run()
    {
        try
        {
            controllerServer.writeLog("client address "+s.getInetAddress());
            InputStream inputStream = s.getInputStream();
            int servId = inputStream.read();
            OutputStream outputStream = s.getOutputStream();
            Thread t;
            switch(servId) {
                case WRITE_EMAIL:
                    controllerServer.writeLog("Writing Email, id : " + WRITE_EMAIL);
                    outputStream.write(SUCCESS_RESPONSE);
                    t = new Thread(new EmailReceiver(s,lock, servId));
                    t.start();
                    break;

                case READ_ALL_EMAILS:
                    controllerServer.writeLog("Loading users list, id : " + READ_ALL_EMAILS);
                    outputStream.write(SUCCESS_RESPONSE);
                    t = new Thread(new EmailReader(s));
                    t.start();
                    break;

                case USER_LIST:
                    controllerServer.writeLog("Caricamento lista utenti, id : " + USER_LIST);
                    outputStream.write(SUCCESS_RESPONSE);
                    t = new Thread(new UserGetter(s));
                    t.start();
                    break;

                case LOGIN:
                    controllerServer.writeLog("Login, id " + LOGIN);
                    outputStream.write(SUCCESS_RESPONSE);
                    t = new Thread(new UserLogin(s));
                    t.start();
                    break;

                case DELETE_EMAIL:
                    controllerServer.writeLog("delete email, id " + DELETE_EMAIL);
                    outputStream.write(SUCCESS_RESPONSE);
                    t = new Thread(new EmailDelete(s));
                    t.start();
                    break;
                default:
                    controllerServer.writeLog("Error");
                    outputStream.write(FAILURE_RESPONSE);
                    break;

            }


        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

}

1 Answers

1
Zephyr On Best Solutions

The problem is that you're creating a new ControllerServer in your other classes.

When you call controllerServer.writeLog() from those classes, you're actually writing to a completely different List than the one that is bound to your ListView.


Instead, you should have just one ControllerServer and pass a reference to it to any other classes that need it.

For example, you could update your ServiceHandler class like this:

public class ServiceHandler extends Thread {
  private static Object lock = new Object();
  private static ServerLog serverLog = new ServerLog();
  private Socket s;
  private ControllerServer controllerServer;  // Do NOT instantiate it here!

  // Pass your single ControllerServer to the constructor
  public ServiceHandler (Socket socket, ControllerServer controllerServer) {
    this.s = socket;
    this.controllerServer = controllerServer;

    this.start();
  }
}

Now, when you call controllerServer.writeLog() later, you'll be doing so on the correct instance of ControllerServer.