0

I need to send var Msj from public void run() in ChatAppClienteMod to private void SendMsj (KeyEvent event) in ChatAppClienteController but I get an error:

error: non-static method MensajeRecibido(String) cannot be referenced from a static context ChatAppClienteController.MensajeRecibido(Msj);

Any ideas?

ChatAppClienteMod

 package chatappcliente;

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import javafx.stage.Stage;
public class ChatAppClienteMod extends Thread{
    private int port;
    private String url;
    private boolean bConect;
    private Socket s;

    public ChatAppClienteMod(int port, String url){
        this.port = port;
        this.url = url;
    }

    public void run(){
        try {
            System.out.println(port + url);
            s = new Socket(url,port);
            DataInputStream Entrada = new DataInputStream(s.getInputStream());
            System.out.println("Conect sucess");
            bConect = true;
            while(bConect){
                String Msj = Entrada.readUTF();
                ChatAppClienteController.MensajeRecibido(Msj);
            }
        } catch (NumberFormatException | IOException e) {
            System.out.println("Error");
        }
    }
    public void SendMsj(String Msj){
        try {
            DataOutputStream Salida = new DataOutputStream(s.getOutputStream());
            Salida.writeUTF(Msj);
        } catch (Exception e) {
            System.out.println("No se pudo enviar el mensaje");
        }
    }
}

ChatAppClienteController

package chatappcliente;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
public class ChatAppClienteController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @FXML private TextField tfPuerto;
    @FXML private TextField tfUrl;
    @FXML private TextField tfNick;
    @FXML private TextField tfMsj;
    @FXML private TextArea taUsers;
    @FXML private TextArea taMens;
    @FXML private ChatAppClienteMod ClienteHilo = null;

    @FXML private void Conectar (ActionEvent evt){
        System.out.println("Ok");
        taUsers.setText("Ok");
        int port = Integer.parseInt(tfPuerto.getText());
        String url = tfUrl.getText();
        if(ClienteHilo == null){
            ClienteHilo = new ChatAppClienteMod(port,url);
            ClienteHilo.start();
        }
    }
    @FXML  public void MensajeRecibido (String Msj){
        taMens.setText("hELLLOOOO");
        taMens.appendText(Msj + "\n");

    }  
    @FXML private void SendMsj(KeyEvent event){
        System.out.println(event.getCode());
        if(event.getCode()== KeyCode.ENTER){
            ClienteHilo.SendMsj(tfMsj.getText());
            tfMsj.setText("");
        }
    }

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

}
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
  • 1
    See http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context for why you get the error. You need to give the `ChatAppClienteMod` the reference to the controller *instance* that was loaded by the `FXMLLoader`. – James_D Nov 30 '15 at 21:32
  • 1
    You will also want to use [Platform.runLater](https://docs.oracle.com/javase/8/javafx/api/javafx/application/Platform.html#runLater-java.lang.Runnable-) for the call to the controller instance as the controller method is modifying the active scene graph and the call point is currently off of the JavaFX application thread. – jewelsea Dec 01 '15 at 00:20

0 Answers0