Results 1 to 6 of 6
  1. #1
    JeroenV is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default JFrame updating gui

    Hello,

    I'll try to keep this short and simple.

    In my JFrame gui I have a button that will start a certain operation. I want to loop this operation and after every loop show the progress that has been made, I noticed that .setText won't work unless the entire Event code has been done. Here is the latest of my countless tries:

    Java Code:
    		startButton = new JButton("Start");
    		scan = new scanner();
    		
    		startButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				//Tutorial tut = new Tutorial();
    				//tut.doScript();
    		        StringPanel.startButton.setVisible(false);
    		        
    				if(!scan.scan360()){
    				 //CALL THE EVENT AGAIN TO MAKE A LOOP WHERE AT THE END OF EVERY LOOP THE GUI UPDATES
    				}
    			}
    		});
    in the "scanner" class itself I basically have some code that does certain operations for 5 seconds and inside that code it writes some new text (which will only change as soon as the code and thus the entire event ended):
    Java Code:
            StringPanel.jText.setText(visual);

    Any suggestions?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: JFrame updating gui

    You shouldn't do long-running tasks on the EDT, as it makes your GUI become unresponsive. Instead, look into using a Thread or a SwingWorker.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JeroenV is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: JFrame updating gui

    I did look into Threads and SwingWorker (after googling this problem) but quite honestly I don't understand how to implement all the things from the tutorials I've been reading into this example.

    Basically if you simplify my code (after pressing the start button that calls the event):
    Java Code:
    for(int i = 0;i<=10;i++){
    // Code that takes five seconds
    // Write output into GUI
    }
    However it would only write the output into the gui after the entire code (including the for loop) was done. While actually what I wanted was it to write the output into the gui after each loop so the viewer can see the progress.

    EDIT:
    I've been reading information about that JFrame works with a single Thread and so it can only do one operation at a time and that I should get that thread and write there. But I have no idea how to get the thread.
    Last edited by JeroenV; 10-07-2013 at 08:00 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: JFrame updating gui

    Sounds like a trip to the tutorials is in your near future: Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    JeroenV is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: JFrame updating gui

    Thanks, I'll read these through.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: JFrame updating gui

    No problem. This tutorial might actually be closer to what you're looking for: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Click in JFrame Titlebar sets the JFrame Opaque
    By tim8w in forum AWT / Swing
    Replies: 1
    Last Post: 02-02-2013, 07:29 AM
  2. JFrame frozen while updating data from streams?
    By SmugMug in forum New To Java
    Replies: 2
    Last Post: 05-31-2012, 05:57 AM
  3. JFrame not "updating"
    By Mokomi in forum AWT / Swing
    Replies: 1
    Last Post: 05-28-2011, 04:15 AM
  4. Updating JFrame
    By shadowfax57 in forum AWT / Swing
    Replies: 1
    Last Post: 11-21-2010, 03:01 PM
  5. Replies: 5
    Last Post: 06-14-2010, 06:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •