Trying to update UI with from another thread

Hi,
I start a JavaFX application from another class, and then I want to modify UI components from it. I have been trying to use Platform.runLater to do it.
But the GUI hangs initially (doesnt display anything) for the first 5 seconds (sleep time) and then modifies and shows it.
I want to display the GUI at first, and then after 5 seconds the GUI should update with the message, but with the below code it just hangs first and displays everything after 5seconds.
Here sampleGUI is a a javafx app with text fields in it.
+public class StartGame extends Application {+
+@Override+
+public void start(Stage stage) throws Exception {+
final sampleGUI gui = new sampeGUI();
gui.start(stage);
+Platform.runLater(new Runnable() {+
+@Override+
+public void run() {+
+try {+
System.out.println("Sleeping now...");
Thread.sleep(5000);
System.out.println("Sleep over!");
gui.updateText("New message");
+} catch (InterruptedException ex) {+
System.out.println("exception" ex);+
+}+
+}+
+});+
+}+
+}+

Platform.runLater(new Runnable() {
  @Override
  public void run() {
});causes the Runnable's run method to be executed on the FX Application Thread. Since you put Thread.sleep(5000) inside your Runnable's run method, the sleep happens on the FX Application Thread.
The call to runLater(...) can be invoked from any thread, including the FX Application Thread.
So if you are not in the FX Application thread, you want to do:
// any long-running task, for example
System.out.println("Sleeping now");
Thread.sleep(5000);
System.out.println("Sleep over");
Platform.runLater(new Runnable() {
  @Override
  public void run() {
    // update UI:
    gui.updateText("New Message");
});If you are on the FX Application thread, which is the case in your Application.start(...) method, you need to create a new thread to execute your long-running code. You can do this "by hand", creating a Thread and a Runnable for it to execute, but it's probably better to use the JavaFX concurrency API, which has lots of useful hooks for updating the UI on the FX Application Thread.
In this case, the code would look like this:
public class StartGame extends Application {
  @Override
  public void start(Stage stage) throws Exception {
    final SampleGUI gui = new SampleGUI();
    gui.start();
    final Task<String> waitingTask = new Task<String>() {
      @Override
      public String call() throws Exception {
        System.out.println("Sleeping");
        Thread.sleep(5000);
        System.out.println("Sleep over!");
        return "New Message" ;
    waitingTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
      @Override
      public void handle(WorkerStateEvent event) {
        gui.updateMessage(waitingTask.getValue());
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.submit(waitingTask);
}There are (probably dozens of) other ways to use a Task to do this. See the [url http://docs.oracle.com/javafx/2/api/javafx/concurrent/Task.html]API docs for Task for more.

Similar Messages

  • How to refresh a JTable of a class from another thread class?

    there is an application, in server side ,there are four classes, one is a class called face class that create an JInternalFrame and on it screen a JTable, another is a class the a thread ,which accept socket from client, when it accept the client socket, it deal the data and insert into db,then notify the face class to refresh the JTable,but in the thread class I used JTable's revalidate() and updateUI() method, the JTable does not refresh ,how should i do, pls give me help ,thank you very much
    1,first file is a class that create a JInternalFrame,and on it there is a table
    public class OutFace{
    public JInternalFrame createOutFace(){
    JInternalFrame jf = new JInternalFram();
    TableModel tm = new MyTableModel();
    JTable jt = new JTable(tm);
    JScrollPane jsp = new JScrollPane();
    jsp.add(jt);
    jf.getContentPane().add(jsp);
    return jf;
    2,the second file is the main face ,there is a button,when press the button,screen the JInternalFrame. there is also a thread is beggining started .
    public class MainFace extends JFrame implements ActionListener,Runnable{
    JButton jb = new JButton("create JInternalFrame");
    jb.addActionListener(this);
    JFrame fram = new JFrame();
    public void performance(ActionEvent e){
    JInternalFrame jif = new OutFace().createOutFace(); frame.getContentPane().add(JInternalFrame,BorderLayout.CENTER);
    public static void main(String[] args){
    frame.getContentPane().add(jb,BorderLayout.NORTH);
    frame.pack();
    frame.setVisible(true);
    ServerSokct ss = new ServerSocket(10000);
    Socket skt = ss.accept()'
    new ServerThread(skt).start();
    3.the third file is a thread class, there is a serversoket ,and accept the client side data,and want to refresh the JTable of the JInternalFrame
    public class ServerThread extends Thread{
    private skt;
    public ServerThread(Sokcet skt){
    this.skt = skt;
    public void run(){
    OutputObjectStream oos = null;
    InputObjectStream ios = null;
    try{
    Boolean flag = flag;
    //here i want to refresh the JTable,how to write??
    catch(){}
    4.second is the TableModel
    public class MyTableModel{
    public TableModel createTableModel(){
    String[][] data = getData();
    TableModel tm = AbstractTableModel(
    return tm;
    public String[][] getData(){
    }

    Use the "code" formatting tags when posting code.
    Read this article on [url http://www.physci.org/codes/sscce.jsp]Creating a Simple Demo Program before posting any more code.
    Here is an example that updates a table from another thread:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=435487

  • My XP machine is unable to load iTunes 10.5.1. It gets about halfway loaded then says it can't find Bonjour.msi. However, that file is on the computer. I tried loading the Bonjour.msi from another computer but it came up with the same message.

    My XP machine is unable to load iTunes 10.5.1. It gets about halfway loaded then says it can't find Bonjour.msi. However, that file is on the computer. I tried loading the Bonjour.msi from another computer but it came up with the same message. I also tried to remove iTunes from the add/remove sector on Control Panel but it also refused for the same reason. Help, please.

    Try:                                               
    - iOS: Not responding or does not turn on           
    - Also try DFU mode after try recovery mode
    How to put iPod touch / iPhone into DFU mode « Karthik's scribblings
    - If not successful and you can't fully turn the iOS device fully off, let the battery fully drain. After charging for an least an hour try the above again.
    - Try another cable                     
    - Try on another computer                                                       
    - If still not successful that usually indicates a hardware problem and an appointment at the Genius Bar of an Apple store is in order.
    Apple Retail Store - Genius Bar
    The missing apps could have been done by setting the Restrictions that can hid those apps. If the backup was made with those retrictions set the the Restrictions are also restored.
    Thus, if you get it to work restore to factory settings/new iPod, not from backup                               
    You can redownload most iTunes purchases by:        
      Downloading past purchases from the App Store, iBookstore, and iTunes Store

  • I tried to update my iphoto from 9.2.3 on my MacPro and it is failing to download. I have OS X which is not compatible with it.

    How do I download iphoto? I tried to update my iphoto from 9.2.3 on my MacPro and it is failing to download. I have OS X which is not compatible with it.

    I tried to update my iphoto from 9.2.3 on my MacPro and it is failing to download
    What is the error message?
    How did you originally install iPhoto? If you installed iPhoto from an iLife installer disk or a system installer disk, that came with your Macs, use the updaters on Apple's Software Update page.
    Else update from the Updates tab at the App Store.

  • Im trying to update my apps from the ipad in the app store section and when i press update the ipad tells me that im not connected to the itunes store??

    Im trying to update my apps from the ipad in the app store section and when i press update the ipad tells me that im not connected to the itunes store??

    Saw this on another post.
    Applecare Senior Advisor Txx Bxxx (I have his contact info in an email he just sent) just confirmed with me that the problem people are having with the App Store not loading is an apple issue with there servers, ITS NOT YOUR IPAD so don't go restoring it!   It's not happening to everyone however but they are looking into it, its really hit or miss.
    In the meantime ...........
    The Complete Guide to Using the iTunes Store
    http://www.ilounge.com/index.php/articles/comments/the-complete-guide-to-using-t he-itunes-store/
    Can't connect to the iTunes Store
    http://support.apple.com/kb/TS1368
    iTunes: Advanced iTunes Store troubleshooting
    http://support.apple.com/kb/TS3297
    Best Fixes for ‘Cannot Connect to iTunes Store’ Errors
    http://ipadinsight.com/ipad-tips-tricks/best-fixes-for-cannot-connect-to-itunes- store-errors/
    Try this first - Reset the iPad by holding down on the sleep and home buttons at the same time for about 10-15 seconds until the Apple Logo appears - ignore the red slider - let go of the buttons.
    This works for some users. Not sure why.
    Go to Settings>General>Date and Time> Set Automatically>Off. Set the date ahead by about a year.Then see if you can connect to the store.
     Cheers, Tom

  • I tried to update my ipad from ios 4 to 5 and now it won't connect to my computer - it is fully restored and can't connect - getting an error message. Help!

    I just recently tried to update my ipad from ios 4 to 5 and now I am getting an error message when I try to connect it to the computer so that I can download all of my apps, etc. please help! Error message says iTunes could not connect to this iPad. An unknown error occurred (oxE8000000A).
    Help!

    Syncing with iTunes on a Mac or PC requires:
    Mac: OS X v10.6.8 or later
    PC: Windows 7; Windows Vista; or Windows XP Home or Professional with Service Pack 3 or later
    iTunes 10.7 or later (free download from www.apple.com/nz/itunes/download)

  • HT4972 I am trying to update ipod touch from ios 4.3.2 to ios 6.1.3.  I have it connected to my computer, and the summary in itunes indicates the update is available.  However, when I click "update", it opens Windows Explorer and waits for a file name to

    I am trying to update ipod touch from ios 4.3.2 to ios 6.1.3.  I have it connected to my computer, and the summary in itunes indicates the update is available.  However, when I click "update", it opens Windows Explorer and waits for a file name to open.

    I am not at that computer now, but I did check via windows update & with the check updates menu choice in the itunes program  - neither indicated a newer version of itunes is available. (it is a vista computer).
    The itunes software on the computer displays my ipod & the current ios (4.3.2), and indicates a newer ios is available (6.1.3). When I choose "update", I am taken to Windows Exploer and prompted to choose a file to open. it is as if the "update" button has the incorrect code attached to it.
    The ipod is working, but there are several app I would like to install, but I can't with the current ooperating system.

  • TS3694 Since last few months i am trying to update my ipad from iOS 3.2.2 to 4.1.2 or 5.1, but it always shows error 3002, 3194 etc. I have cheched& resorted to all the given troubleshooting steps , but they didn't help.

    Since last few months i am trying to update my ipad from iOS 3.2.2 to 4.1.2 or 5.1, but it always shows error 3002, 3194 etc. I have checked & resorted to all the given troubleshooting steps , but they didn't help.
    Somebody please help me

    Connect the iPad to iTunes, and do the update with iTunes.
    The 4.1.2 ipsw file will not work, no longer supported.
    The incremental update directly on the iPad does not work either, unless you are already on iOS 5.
    Let iTunes do the job, you already lost too much time by doing it yourself.

  • HT201210 I am trying to update iphone 4 from ISO 5.1.1 to ISO 6.0.1

    I am trying to update iphone 4 from iSO 5.1.1 to iSO 6.0.1.  The phone says it needs a software update, but when it's almost finished downloading it says Update failed.  I am connected to Wi-Fi.  Any suggestions?

    Are you updating over the air or using iTunes on your computer? If you're doing it over the air, use iTunes instead. It's a little more reliable as far as the download goes.
    If you're doing it via iTunes, disable your antivirus and firewall software. It can interfere with the update.

  • HT201442 hi, i am trying to update my Ipad2 from ios 4.3.3 to ios 7.0.4 i donwloaded the file but i am getting error 3194 and i cant update it what can i do

    Hi, I am trying to update my ipad2 from ios 4.3.3 to ios 7.0.4 after i have downloaded the update it cant update my ipad it gave me error 3194
    what can i do. Thanks

    iPad: Basic troubleshooting
    http://support.apple.com/kb/TS3274
    Update and restore alert messages on iPhone, iPad, and iPod touch
    http://www.buybuyla.com/tech/view/012953a0d412000e.shtml
    iOS: Resolving update and restore alert messages
    http://support.apple.com/kb/TS1275
    iPad: Unable to update or restore
    http://support.apple.com/kb/ht4097
    iTunes: Specific update-and-restore error messages and advanced troubleshooting
    http://support.apple.com/kb/TS3694
     Cheers, Tom

  • My ipod touch 5 wount let me back in after trying to update ios with my computer and now says connect to itunes. it still wount work when i plug it in and click restore for it?

    my ipod touch 5 wount let me back in after trying to update ios with my computer and now says connect to itunes. it still wount work when i plug it in and click restore for it?

    it went through the extracting software stage but then an error message saying that it encountered problems came up and it didn't restore.

  • HT4623 I am trying to update my ipad from ios 4.3 but when connected to itunes I get an  error messagesaying that the internet is not accessible when it is. How do I resolve this?

    Hi
    I am trying to update my ipad from ios 4.3 but when connected to itunes I get an  error message saying that the internet is not accessible when it is. How do I resolve this?

    Disable your firewall and antivirus software, restart your iPad, quit iTunes, restart your computer and then try to update again.

  • My iTouch will not restore becuase for some reason it won't reconize it. I merely tried to update it with the newest software and every thing went to sh--. Now it is just stuck on the screen showing the apple sign. Can anyone help me.

    My iTouch will not restore becuase for some reason it won't reconize it. I merely tried to update it with the newest software and every thing went to sh--. Now it is just stuck on the screen showing the apple sign. Can anyone help me.

    There's a whole lot to read in your post, and frankly I have not read it all.
    Having said that, this troubleshooting guide should help:
    http://support.apple.com/kb/TS1538
    In particular, pay attention to the mobile device support sections near the bottom, assuming you have already done the items above it.

  • Can i catch an exception from another thread?

    hi,guys,i have some code like this:
    public static void main(String[] args) {
    TimeoutThread time = new TimeoutThread(100,new TimeOutException("超时"));
    try{
    t.start();
    }catch(Exception e){
    System.out.println("eeeeeeeeeee");
    TimeoutThread will throws an exception when it runs ,but now i can't get "eeeeeeeeeee" from my console when i runs the main bolck code.
    i asked this question in concurrent forums,somebody told me that i can't.so ,i think if i can do this from aspect of jvm.
    thank you for your help
    Edited by: Darryl Burke -- Double post of how to catching exceptions from another thread locking

    user5449747 wrote:
    so ,i think if i can do this from aspect of jvm. What does that mean? You think you'll get a different answer in a different forum?
    You can't catch exceptions from another thread. It's that easy. You could somehow ensure that exceptions from that other thread are always caught and somehow passed to your thread, but that would be a different thing (you would still be catching the exception on the thread it is originating from, as is the only way).
    For example you can use setUncaughtExceptionHandler() on your thread to provide an object that handles an uncaught exceptions (and you could pass that uncaught exception to your other thread in some way).

  • HT1338 im trying to update my macbook from 10.4.11 but its not letting me. what should i do?

    im trying to update my software from 10.4.11 but its not letting me. what should i do?

    You are at the max version of Tiger OS 10.4.11. There are no additional updates.
    If you want to go to a higher version of Mac OS X, that's an upgrade, that requires a purchase.
     Cheers, Tom

Maybe you are looking for

  • How does one drag a song to a new album?

    How does one drag a song from one album and drop it into another album? Many of the mp3 files that I use are ones that I create myself for my students. It would be great if I could drag them into the correct album without having to type in the inform

  • Generic Sync: How to register 2 Outbound Methods

    Hello, i am developing a generic sync application. I have registered a method for inbound+outbound. It works well.Now i have a second method in the same application. I made the mapping in BWAFMAPP,but when i try to create a outbound container for the

  • Technical cutover - Dev to Prod ChaRM (RABAX_STATE error ZMCR)

    Hi I have just performed a technical cutover from Development Solman to Production Solman. All transports imported successfully, but I am getting the below error in Production when trying to create ZMCR (change request) in CRM Web Client UI: This err

  • Hi Adobe Photoshop Cs6 Website Making?

    English ; Hi My question turkey to make car rental company in our website CAN USE Adobe Photoshop CS6 software. I did a little experiment and ICEL from this address. Address: http://www.malkoclaroto.com Türkçe; Merhabalar Benim sorum türkiye'de oto k

  • My Indesign Crashes When Opening a indesign file.

    I worked a file quark to indesign conversion. Copy the Text to Indesign Template. After finishing the Document I saved it and close it. When I try to open that file Indesign crashed Message appears "The exception unknown software exception (0xc000000