-1

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);

    }
}
artek
  • 61
  • 8

1 Answers1

-1

You are trying to set the message using setMessage2() right? Then why don't you access Label labelChatin that method and set text using Platform.runLater(Runnable)

  • `private void setMessage2(String message) { Platform.runLater(() -> { Class-obj.labelChat.setText(message); }); } ` and make `@FXML private Label labelChat;` to `@FXML public Label labelChat;` – Atish Abhang Nov 02 '17 at 17:14
  • It says that non-static variable labelChat cant be referenced from a static context – artek Nov 02 '17 at 17:27
  • Check your scope. – Atish Abhang Nov 02 '17 at 18:09
  • You are trying to access a non-static variable i.e `Label labelChat` from main method which is obviously a static. Either make `labelChat` `static` or call `messagePrint()` from non-static method. – Atish Abhang Nov 02 '17 at 20:36
  • You can't make `labelChat` static, as it's `@FXML`-injected. – James_D Nov 03 '17 at 00:01