Hey, I'm working on my first JavaFX GUI for a Torrent Client I'm working on. I'm a little confused by the program flow.
In the code bellow you can see what I tried to do in the block for the File Select Button. I wanted to output text to the TextArea as the program runs updating the user on whats happening as some of the methods take a long time. You can see "textbox.setText("Contacting Tracker...");" and "textbox.setText("Getting peers...");". Obviously at that point the program has not reached Stage.show() so they are never outputted.
I know the way I have tried to do that is completely wrong and I have no idea of the correct way to implement it. I also would want TableView to be updated as soon as a new item is added to the ObservableList - displaylist not after all the code that follows it.
public class Main extends Application { int lastindex = 0; @Override public void init(){ } @Override public void start(Stage stage){ //crate list of torrents TorrentList torrentList = new TorrentList(); ObservableList<Display> displaylist = FXCollections.observableArrayList(); TableView<Display> table; //Text Box TextArea textbox = new TextArea(); textbox.setPrefRowCount(4); textbox.setPrefHeight(150); textbox.setPrefWidth(300); //file select button FileChooser fileChooser = new FileChooser(); Button openfile = new Button("Open Torrent"); openfile.setOnAction(e -> { File selectedFile = fileChooser.showOpenDialog(stage); torrentList.Add(selectedFile); lastindex = torrentList.getLast(); displaylist.add(new Display()); displaylist.get(lastindex).setId(lastindex); displaylist.get(lastindex).setName(torrentList.List.get(lastindex).getName()); textbox.setText("Contacting Tracker..."); torrentList.List.get(lastindex).printAll(); torrentList.List.get(lastindex).tracker.printall(); textbox.setText("Getting peers..."); torrentList.List.get(lastindex).tracker.getPeers(); torrentList.List.get(lastindex).tracker.peers.printpeers(); textbox.setText(""); }); //table columns TableColumn<Display, Integer> idcolumn = new TableColumn<>("ID"); idcolumn.setMinWidth(100); idcolumn.setCellValueFactory(new PropertyValueFactory<Display, Integer>("id")); TableColumn<Display, String> namecolumn = new TableColumn<>("Name"); namecolumn.setMinWidth(300); namecolumn.setCellValueFactory(new PropertyValueFactory<>("name")); //setup table table = new TableView<>(); table.setItems(displaylist); table.getColumns().addAll(idcolumn, namecolumn); HBox root = new HBox(); root.getChildren().addAll(table, openfile, textbox); Scene scene = new Scene(root, 900, 250); stage.setTitle("Rabbit Torrent"); stage.setScene(scene); stage.show(); } @Override public void stop(){ } }
Messing around with the GUI made me realise that the main guts of the actual program (not implemented above yet) need to run in a loop. I have no idea of how to have a main program loop inside the javaFX GUI code.