Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: JavaFX - Main Program Loop

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JavaFX - Main Program Loop

    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.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: JavaFX - Main Program Loop

    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JavaFX - Main Program Loop

    Quote Originally Posted by Norm View Post
    Yeah that was me. I posted on both forums.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: JavaFX - Main Program Loop

    The problems with cross-posting

    I think the other forum has a similar policy.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JavaFX - Main Program Loop

    ^Ah I didn't know. Want me to delete one?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: JavaFX - Main Program Loop

    No. It's OK to post on more than one forum as long as every one knows about the other forums.
    That allows everyone to check if a solution has already been found somewhere else or if there is a hole that needs filling.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: JavaFX - Main Program Loop

    I had a similar disappointment with the way JavaFX expects you to start your program. The init() function is nice for background initialization, but try showing a GUI splash screen from there. Not happening!

    What I did was make the start() function just spawn a new thread for all the background initialization work. Then the worker thread calls stage.show() near the beginning and then has a visible GUI to update while it's working. Remember to use Platform.runLater(Runnable r) when updating JFX components (including showing stages) from a non-GUI thread.

Similar Threads

  1. Replies: 0
    Last Post: December 7th, 2017, 11:55 AM
  2. With out main class run a program
    By sudhakarcse11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 9th, 2014, 06:01 AM
  3. "Could not find the main class: main. program will exit" - Wierd case.
    By AvivC in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 25th, 2014, 06:44 PM
  4. Replies: 5
    Last Post: August 11th, 2011, 12:39 PM
  5. Stuck on a few Statements/Questions on timing of Main Game Loop.
    By StevenW in forum Java Theory & Questions
    Replies: 3
    Last Post: July 19th, 2011, 09:20 AM