JavaFX - locked screens

1

All JavaFX screens crash when the server is powered on.

I tried to use a task but it does not seem to work. The program works even with the screens locked but I would like to be able to access the interface while the server is turned on.

I've been part of code in this example .

The control class follows:

public class FXMLTelaServidorController implements Initializable {

    //variaveis usadas no controle do servidor

    private static ServerSocket server;
    private static ArrayList<BufferedWriter>clientes;
    private String nome;
    private Socket con;
    private InputStream in;
    private InputStreamReader inr;
    private BufferedReader bfr;


    //variaveis usadas na parte de visualizaçao
    @FXML
    private Pane painel;

    @FXML
    private Label labelLigarServidor;

    @FXML
    private Label labelIP;

    @FXML
    private Label labelPorta;

    @FXML
    private TextField campoTextoIP;

    @FXML
    private TextField campoTextoPorta;

    @FXML
    private Button botaoLigar;

    @FXML
    private Button botaoDesligar;


    /**
     * Método construtor(seria se o javafx aceitasse thread de boas) como nao aceitou transformei em um metodo normal
     * @param con1 
     */

    public void FXMLTelaServidorController(Socket con1){
        this.con = con1;
        try {
            in  = con.getInputStream();
            inr = new InputStreamReader(in);
            bfr = new BufferedReader(inr);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    @FXML
    void ligarServidor(ActionEvent event) {

        try{
            //Cria os objetos necessário para instânciar o servidor
            campoTextoPorta.getText();

            Object[] texts = {"porta do servidor", campoTextoPorta.getText()};
            JOptionPane.showMessageDialog(null, texts);
            server = new ServerSocket(Integer.parseInt(campoTextoPorta.getText()));
            //clientes = new ArrayList<BufferedWriter>();
            JOptionPane.showMessageDialog(null,"Servidor ativo na porta: "+
                    campoTextoPorta.getText());


                System.out.println("Aguardando conexão...");
                Socket con1 = server.accept();
                System.out.println("Cliente conectado...");
            //inicio da task
                Task<String> servidor = new Task<String>() {
                @Override
                protected String call() throws Exception {
                    return "deu certo";
                }  
                @Override
                 protected void succeeded() {

                     JOptionPane.showMessageDialog(null, this);
                }


            };
            //fim da task    
                FXMLTelaServidorController(con1);
                Thread t =  new Thread(servidor);
                t.setDaemon(true);
                t.start();
                Platform.runLater(t);

            //codigo pego do run
            try{

            String msg;
            OutputStream ou =  con.getOutputStream();
            Writer ouw = new OutputStreamWriter(ou);
            BufferedWriter bfw = new BufferedWriter(ouw);
            clientes = new ArrayList<BufferedWriter>();
            clientes.add(bfw);
            nome = msg = bfr.readLine();

            while(!"Sair".equalsIgnoreCase(msg) && msg != null)
            {
                msg = bfr.readLine();
                sendToAll(bfw, msg);
                System.out.println(msg);
            }

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

        }
            //fim do codigo pego do run


        }catch (Exception e) {

            e.printStackTrace();
        }




    }

    @FXML
    void desligarServidor(ActionEvent event) {
        Thread.currentThread().interrupt();
    }



    @Override
    public void initialize(URL url, ResourceBundle rb) {




    }

    /***
     * Método usado para enviar mensagem para todos os clients
     * @param bwSaida do tipo BufferedWriter
     * @param msg do tipo String
     * @throws IOException
     */
     public void sendToAll(BufferedWriter bwSaida, String msg) throws  IOException{
        BufferedWriter bwS;

        for(BufferedWriter bw : clientes){
            bwS = (BufferedWriter)bw;
            if(!(bwSaida == bwS)){
                bw.write(nome + " : " + msg+"\r\n");
                bw.flush();
            }
        }
    }


}
    
asked by anonymous 19.05.2018 / 21:36

1 answer

1

Here's the change I made to the method to work:

@FXML
void ligarServidor(ActionEvent event) throws IOException {



            //inicio da task
            Task servidor = new Task() {

               @Override
                protected Object call() throws Exception {

                    //codigo pego do run
                    try {
                     //Cria os objetos necessário para instânciar o servidor

                     Object[] texts = {"porta do servidor", campoTextoPorta.getText()};
                     JOptionPane.showMessageDialog(null, texts);
                     server = new ServerSocket(Integer.parseInt(campoTextoPorta.getText()));
                     JOptionPane.showMessageDialog(null, "Servidor ativo na porta: "
                             + campoTextoPorta.getText());
                     JOptionPane.showMessageDialog(null, Platform.isFxApplicationThread());
                     System.out.println("Aguardando conexão...");

                     Socket con1 = server.accept();
                     FXMLTelaServidorController(con1);
                     System.out.println("Cliente conectado...");
                     String msg;
                     OutputStream ou = con.getOutputStream();
                     Writer ouw = new OutputStreamWriter(ou);
                     BufferedWriter bfw = new BufferedWriter(ouw);
                     clientes.add(bfw);
                     nome = msg = bfr.readLine();

                     while (!"Sair".equalsIgnoreCase(msg) && msg != null) {
                         msg = bfr.readLine();
                         sendToAll(bfw, msg);
                         System.out.println(msg);
                     }
                 } catch (IOException ex) {
                     Logger.getLogger(FXMLTelaServidorController.class.getName()).log(Level.SEVERE, null, ex);
                 }
                    //fim do codigo pego do run
                    return null;



                }

           };
            //fim da task    

             javafx.application.Platform.runLater(() -> {
                 Thread t = new Thread(servidor);
                 t.start();
             });



        } 
    
20.05.2018 / 17:37