0

I've run in to some troubles and couldn't really find an answere.

I need to often add Strings to a TextArea. The Strings come from other classes/objects.

My idea:

@FXML
private static TextArea TAactivity;

public static void activity(String s) {
    TAactivity.appendText(s);
}

and write to it from other classes with:

MainController.activity("Random stuff");

What am I doing wrong?

Greetings

//Edit

public class Main extends Application {
private final int PORT = 4000;
private final String SERVER = "127.0.0.1";
public static Client client;
@Override
public void start(Stage primaryStage) throws Exception{
    client = new Client(SERVER, PORT);
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("JJFTP Client");
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
  }
}
public class Client {

private String server;
private int port;

public Client(String s, int p) throws InterruptedException, IOException {
    server = s;
    port = p;
}
public String messagefromserver() throws IOException {
    Controller.c.activity("Waiting for message from server");
    StringBuilder sb;
    Socket socket = new Socket(server, port);
    DataInputStream ASCIIin = new DataInputStream(socket.getInputStream());
    int inputlength = ASCIIin.readInt();
    sb = new StringBuilder();
    for (int i = 0; i < inputlength; i++) {
        int temp = ASCIIin.readInt();
        if (temp < 0) {
            throw new IllegalArgumentException();
        }
        sb.append((char) temp);
    }
    Controller.c.activity("Received -" + sb + "- from server");
    ASCIIin.close();
    socket.close();
    return sb.toString();
  }
}
public class Controller implements Initializable {

public static Controller c;

@FXML
private TextArea TAfiles;
@FXML
private TextField TFcmd;
@FXML
private TextArea TAactivity;

@FXML
public void send() throws Exception {
    String s = TFcmd.getText();
    TFcmd.setText("");
    Main.client.messagetoserver(s);
    String answere = Main.client.messagefromserver();
    if (answere.equals("FILE")) {
        StringBuilder sb = new StringBuilder();
        sb.append(s);
        sb.delete(0, 9);
        Main.client.downloadfile(String.valueOf(sb));
        updatefilelist();
        activity("Filelist updated");
    }
}

public void activity(String s) {
    System.out.println(s);
    TAactivity.setText(TAactivity.getText() + "\n" + s);
}

@Override
public void initialize(URL location, ResourceBundle resources) {
    c=new Controller();
    try {
        updatefilelist();
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}

There is of course more methods, but I guess not all of them are neccessary. If I remove the static from the objects for Client and or Controller, it says it can not refer to something static from a not static context.

  • Why do so many people seem to think that making things `static` is a way to make them accessible? `static` changes the *scope*, not the accessibility. You simply need to make the controller (which is, of course, a specific object, not a class) available to the other objects that are adding the strings to the text area; then they can call the (non-static) `activity(...)` method on the controller. – James_D Feb 02 '17 at 02:06
  • ok. How do I make the controller accessible to other objects? – Jonathan Hoffmann Feb 02 '17 at 10:50
  • Get a reference to it from the `FXMLLoader`, and pass it to those other objects. See, e.g. http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – James_D Feb 02 '17 at 11:07
  • I have no clue on how to do that. I start the FXMLloader in the main class. Furthermore, The string is getting passed on to my method, because with System.out.println() it prints them. It just crashes with a java.lang.NullPointerException when I try to print it from another class or object. If I call the method from the controller itself it is no problem. – Jonathan Hoffmann Feb 02 '17 at 12:37
  • [Edit] your question to show how you are trying to call the method on the controller from other objects (without making anything static, obviously). – James_D Feb 02 '17 at 15:10

0 Answers0