0

I am very new to JavaFX. I am developing a desktop app using JavaFX and scene builder with Netbeans IDE.

Here is my application UI in words (I have less reputation to show screenshot).

  1. One check box named "Click here to enable textbox" (present in right side of the checkbox)

  2. One textbox (this should be disabled by default, if user click/check on the checkbox, it should have write option; i.e. enabled)

  3. one button name "Do the big task" - This button performs main task, this task is quite long. (A ssh connection is done and script is called from a Java class to a remote machine, this scripts calls another two scrpts and finally done and all process's logs are recorded in a file named output.log )

  4. another button named "show logs" (intention is to show output.log live [as writing at the time of execution] ) followed by a large text field (where logs should be shown)

  5. and a progress bar (where calling from script1 to script n progress will be shown)

Below is my FXMLDocumentController.java class:

@FXML
Button btnDotheBigtask;

@FXML 
Button btnShowLogs;

@FXML 
TextArea txtShowLogs;

@FXML 
TextField txtMyText;

@FXML 
CheckBox clickHeretoEnableTextBox;


@FXML
public void btnDotheBigtaskClick(ActionEvent event) throws IOException {        
    //CreateRelease create = new CreateRelease();
    MyClassImpl m = new MyClassImpl();
    m.doTheWork();

}

@FXML
public void btnShowLogs(ActionEvent event) throws IOException {        


}

 @FXML
public void clickHeretoEnableTextBox(ActionEvent event) throws IOException {

}

@FXML
public void txtAreaShowLogs(ActionEvent event) throws IOException {

}

@FXML
public void txtMyText(ActionEvent event) throws IOException {

}

MyClassImpl.java to perform the main task [I have used SSHExecute 1.0 jar to establish ssh connection]

        ConnBean cb = new ConnBean("RemoteServer", "user","password");
        // Put the ConnBean instance as parameter for SSHExec static method getInstance(ConnBean) to retrieve a singleton SSHExec instance
        ssh = SSHExec.getInstance(cb);          
        //Connect to server
        ssh.connect();

       CustomTask sampleTask = new ExecCommand("/usr/Test/myscript.sh");


        //Execution of main taks
        Result rs = ssh.exec(sampleTask);

What I want to do is :

  1. By default the textbox should disbaled, at clicking in checkbox it should enabled.
  2. On clicking btnDotheBigtaskClick button the UI should not be freeze, when I click on this button A message should be shown in a label something like "work is started... " (That I will create later) and after completion of the whole task label value should be updated to "Work Done".
  3. On clicking on Show logs button, logs should stared showing in the text feild. (UI should not freeze at that time)

Basically I have performed a lot of task to achive this and check where I failed

  1. The big task executes smoothly (As I have used a java standard thread) but failed to show the logs, when I clicked on show log button UI gets freeze. (On showLogs I have read the log file and tried to print in the text feild)

  2. I failed to show/set the work finised/started messsage from a different class (MyClassImpl)in a label present in controller class.

Can anybody help me with concepts or code to achieve this task (code will be much easier to understand).

halfer
  • 19,824
  • 17
  • 99
  • 186
MonsterJava
  • 423
  • 7
  • 23
  • 1
    I suggest you run the long running task in a background thread and display the results only when finished. – Peter Lawrey Jan 10 '16 at 19:21
  • Here I faced two problems, 1. I dont know how to set a text in label present in Controller from MyClassImpl.java beacuse my thread will finish here at MyClassImpl.java another thing 2. If I join the thread UI will freeze until the process finish. – MonsterJava Jan 10 '16 at 19:24
  • 1
    So don't join, this defeats the purpose of having a background thread. You set the text in the label by adding a task to the GUI thread to update the label. Here is an example http://stackoverflow.com/a/20498014/57695 – Peter Lawrey Jan 10 '16 at 19:29
  • Great help, but how to set the label text from MyClassImpl.java because I dont have any idea about the execution time of this task running in MyClassImpl.java. – MonsterJava Jan 10 '16 at 19:40
  • You can only call a method on an object you have a reference to. If you don't have such a reference you will either need to pass one or I suggest pass a Consumer so the caller can decide what to do when you have a result. – Peter Lawrey Jan 10 '16 at 19:49
  • 3
    There are multiple questions/answers about that issue (e.g. [Constantly Update UI in Java FX worker thread](http://stackoverflow.com/questions/20497845/constantly-update-ui-in-java-fx-worker-thread)). – fabian Jan 10 '16 at 20:49
  • Yes I know, but I failed to implement as per the given suggestion, thats why I have elaborately explained here. – MonsterJava Jan 10 '16 at 20:57

0 Answers0