I am making Twitch chat using twitch api wrapper and JavaFx. But I have a problem with passing parameters when getting a message in chat. I made a thread. It should work like this:
- 1) A programm launches
- 2) Starting new "Task"(new Thread in main method) to start a bot
- 3) Getting a message via onMessage method
- 4) Changing the label to the message text
The problem is that i can't understand how to pass Message String from onMessage method to Controller so I can change the label's text.
Here's the code:
Bot class:
public class Bot extends TwitchBot {
public Bot() {
}
public static String message2;
@Override
public void onMessage(User user, Channel channel, String message) throws IOException {
System.out.println("Message is:" + message);
setMessage2(message);
}
private void setMessage2(String message) {
message2 = message;
}
}
Controller.java
public class Controller implements Initializable {
@FXML private Label labelChat;
@FXML private TextArea textArea;
@FXML private GridPane gridPane;
@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println("MADE");
}
public void messagePrint(String message) {
System.out.println("LABEL!!!");
labelChat.setText(message);
}
}
Main.java
public class Main extends Application {
public static Controller c;
@Override
public void start(Stage primaryStage) throws Exception{
System.out.println("Launched");
// Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = loader.load();
Controller c = loader.getController();
Main.c = c;
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 450, 550));
primaryStage.show();
}
public static void main(String[] args) {
TwitchBot bot = new Bot();
bot.connect();
bot.joinChannel("#artek_tv");
Task task = new Task() {
@Override
protected Object call() throws Exception {
bot.start();
return null;
}
};
new Thread(task).start();
Task task2 = new Task() {
@Override
protected Object call() throws Exception {
c.messagePrint(Bot.message2);
return null;
}
};
new Thread(task2).start();
launch(args);
}
}