Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8505819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:25:38+00:00 2026-06-11T02:25:38+00:00

Newbie question about JavaFX that I haven’t been able to answer, despite knowing it

  • 0

Newbie question about JavaFX that I haven’t been able to answer, despite knowing it must be pretty simple to do and not finding any resources on it anywhere I’ve looked (tutorials, many of the Oracle online docs, articles, the well-known JavaFX bloggers, etc.)

I’m developing a command line (script) running application and I have successfully gotten output (via ProcessBuilder) from the script that I can display in an ongoing manner, as things happen on the command line. That is, I can do System.out.println(line); all day long, showing the output in the console, which simply returns output from an input stream returned by the ‘myProcess’ that’s running, created like this:

BufferedReader bri = new BufferedReader(new InputStreamReader(myProcess.getInputStream()))

So I am able to see all the output coming back from the script.

I’d like to set-up a JavaFX TextArea or ScrollPane or, not sure what, to display this output text (there’s a lot of it, like several thousand lines) as an ongoing ‘progress’ of what’s taking place in the script, as it happens. I have a Scene, I have buttons and get input from this scene to start the script running, but now I’d like to show the result of clicking the button “RUN THIS SCRIPT”, so to speak.

I assume I need to create a TextArea as described here or perhaps a TextBuilder would be useful to begin making it. Not sure.

I need a bit of help in how to setup the binding or auto-scroll/auto-update part of this.

Can someone provide me a place to start, to do this with JavaFX? I’d rather not use Swing.

(I’m using JavaFX 2.2, JDK 1.7u7, all the latest stuff, and yes, this is an FXML app–so doing it that way would be preferred.)

UPDATE: Sergey Grinev’s answer was very helpful in the binding part. But here is some more detail on what I mean when I ask for “a bit of help in how to setup” — basically, I need to return control to the main Scene to allow the user to Cancel the script, or to otherwise monitor what’s going on. So I’d like to “spawn” the process that runs that script (that is, have some kind of ‘free running process’), but still get the output from it. (I wasn’t very clear on that in my initial question.)

The technique I’m using here (see below) is to do a waitFor on the process, but of course this means the dialog/Scene is ‘hung’ while the script executes. I’d like to gain control back, but how do I pass the ‘p’ (Process) to some other controller piece (or alternatively, simply kick off that other process passing in the parameters to start the script and have it start the script) that will then do the auto-update, via the binding Sergey Grinev mentions–without ‘hanging’ the Scene/window? Also: Can I then ‘stop’ this other process if the user requests it?

Here is my current code (‘waits’ while script–which takes 20-40 min to run!–completes; this is not what I want, I’d like control returned to the user):

public class MyController implements Initializable {  
  @FXML
  private void handleRunScript(ActionEvent event) throws IOException {

    ProcessBuilder pb = new ProcessBuilder("myscript.sh", "arg1", "arg2", ...);

    Process p = pb.start();
    try {
      BufferedReader bri = new BufferedReader
        (new InputStreamReader(p.getInputStream()));
      String line;

      while ((line = bri.readLine()) != null) {
        System.out.println(line);
        textAreaRight.setText(line);
      }
      bri.close();
      p.waitFor();
    }
    catch (Exception err) {
      err.printStackTrace();
    }    
  }

  @FXML
  private void handleCancel(ActionEvent event) {
    doSomethingDifferent();
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:25:40+00:00Added an answer on June 11, 2026 at 2:25 am
    1. To log strings you can use TextArea
    2. To make it asynchronious you need to make a separate thread for output reader.

    public class DoTextAreaLog extends Application {
    
        TextArea log = new TextArea();
        Process p;
    
        @Override
        public void start(Stage stage) {
            try {
                ProcessBuilder pb = new ProcessBuilder("ping", "stackoverflow.com", "-n", "100");
    
                p = pb.start();
    
                // this thread will read from process without blocking an application
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //try-with-resources from jdk7, change it back if you use older jdk
                            try (BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
                                String line;
    
                                while ((line = bri.readLine()) != null) {
                                    log(line);
                                }
                            }
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }).start();
    
                stage.setScene(new Scene(new Group(log), 400, 300));
                stage.show();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    
        }
    
        @Override
        public void stop() throws Exception {
            super.stop();
            // this called on fx app close, you may call it in user action handler
            if (p!=null ) {
                p.destroy();
            }
        }
    
        private void log(final String st) {
            // we can access fx objects only from fx thread
            // so we need to wrap log access into Platform#runLater
            Platform.runLater(new Runnable() {
    
                @Override
                public void run() {
                    log.setText(st + "\n" + log.getText());
                }
            });
        }
    
        public static void main(String[] args) {
            launch();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a newbie question about jQuery. Why is it that I can trigger
Newbie question about PostgreSQL. I'm migrating a MySQL/PHP app I've created, hosted on a
I have a newbie question about integrating two iOS apps. I have created an
A bit of a newbie question for iOS, just started learning about Core Graphics
I'm a Ruby newbie. Have a very basic question about static and instance variables.
Newbie question: I just installed VisualSVN Server and created a repository. I noticed that
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
I have a newbie question about the following: - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
Sorry about this newbie question. Is there a way to execute multiple sentences at
I'm starting with developing, sorry about this newbie question. I need to create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.