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 3 of 3

Thread: Run code to scan file system after rendering Swing GUI window

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Run code to scan file system after rendering Swing GUI window

    I'm new to using Swing, so I'm not exactly sure how to correctly implement what I'd like to do so I'll explain what I'm trying to do...

    I've written some code that scans a file system for certain file types on start. I'm now going back and trying to rework this to use a GUI rather than simply run in a console. What I'd like to do is render the GUI on the screen (with a jList) and then execute the scan (with the scan updating progress in the GUI and adding the returned file paths to the jList).

    From what I'm able to tell, simply adding a call to my scan method will basically freeze the GUI so I'll likely need to create another thread and update the GUI from there? Any tips or suggestions? Do I make the call for the scan from main() or somewhere else?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Run code to scan file system after rendering Swing GUI window

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.
    From what I'm able to tell, simply adding a call to my scan method will basically freeze the GUI
    How are you able to tell this? Yes, that is not desirable behavior, and we can probably help you fix it, but we need to see what you've done to cause it. There are multiple considerations: Swing is not thread safe, the Swing app should be run on the EDT, other application elements supporting the GUI may be run on other threads, including the main thread, or if the other elements are not independent of the GUI, then look at using SwingWorker. All of these considerations are well documented throughout the 'net.

    If you do return with code, please post it correctly using code or highlight tags per the above link.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Run code to scan file system after rendering Swing GUI window

    I would advice you to write your own ListModel implementation. This custom ListModel of yours will need to synchronized because you are going to add elements to it from another thread.
    You should also read up on how to make Swing thread safe. Basically, when firing Events in your custom ListModel (insertion-, removal- and change events) you need to invoke them on the EventDispatcherThread (EDT).

    As you said, you also need a worker thread to do the actual work.

Similar Threads

  1. Change JAVA Swing GUI Window in NetBeans
    By tazeunite00 in forum Java IDEs
    Replies: 4
    Last Post: August 29th, 2014, 02:12 AM
  2. Change JAVA Swing GUI Window in NetBeans
    By tazeunite00 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 29th, 2014, 12:58 AM
  3. NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
    By Harry998 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 20th, 2013, 11:24 AM
  4. Simple swing GUI code....Plz help
    By Arati2512 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 13th, 2012, 11:13 AM
  5. SWING GUI CONSOLE WINDOW DISPLAY
    By Khadafi in forum Java Theory & Questions
    Replies: 5
    Last Post: January 10th, 2012, 09:15 AM