Threading Swing, is this correct?

Hey guys
Ive just done a little swing window, that is thread safe... well i think it is, im asking you guys if this is the correct way to create a swing app (composition rather than inheritance) and if i add more comonents will they each (e.g JButton) need to be implemented as threads?
any help and your experience would be great
cheers
This is the code for a simple window, Is it how you pros would do it?:
import javax.swing.JFrame;
import java.lang.*;
public class UI implements Runnable
private JFrame wnd = new JFrame("CLIENT");
public void createWnd()
wnd.setSize(400, 400);
wnd.setDefaultCloseOperation(wnd.EXIT_ON_CLOSE);
wnd.setVisible(true);
public void run()
createWnd();
System.out.println("Thread started");
public boolean closed()
if((wnd.getDefaultCloseOperation()) == wnd.EXIT_ON_CLOSE)
return true;
return false;
//System.out.println("pressed closed");
public static void main(String[] args)
UI appWindow = new UI();
Thread t = new Thread(appWindow);
t.start();
if(appWindow.closed())
t = null;
System.out.println("Thread stopped");
} Message was edited by:
FatClient

So the reason that a swing application needs to be on
the EDT of each swing UI (and not on a user thread)
is because thats the queue that holds keystrokes,
mouse clicks, etc that may need the UI to be redrawn
after an event.
And if you had other code going on, this would not be
interrupted/paused whilst the EDT/GUI were working to
redraw a bit of the app - Bcause it is in a THREAD.Swing sets up the EDT (Event Dispatch Thread), and what it does is
pretty simple: it dequeues swing events like mouse events and
keyboard events, etc... and causes the appropriate listener methods
to be called. So for example, ActionListener's actionPerformed methods
are going to be called on the EDT (if you coded correctly).
In Java you are free to create other threads, but you always have to
be careful when multiple threads access common data. In the case
of Swing, to keep its implementation as simple as possible, most
Swing methods are meant to only be called from the EDT.
Please let me know if thats why i should blindly
listen to the JDK tutorials as if it were some sort
of bible/Torah/Quran The Java Language Specification (JLS) is the Koran. The tutorials are the Hadith :-)

Similar Messages

  • Threads + Swing = Confusion

    Hi all,
    I am having a problem with threads and Swing apps. I have read the tutorial 3 times and get more confused each time i try to understand how to update the gui with the other thread.
    At this point I'm just trying to do something simple (eventually i want the gui to report all the progress of the action): get a progress bar to be updated through a Thread Class. I have the gui built with a button and a progress bar. Here is the codei have for the simpleThread class
    import java.awt.*;
    import javax.swing.*;
    public class simpleThread extends Thread{
        public simpleThread(String str) {
            super(str);
        public void run() {
            for (int i = 0; i<1000000; i++) {
                final int x = i;
                Runnable getTextFieldText = new Runnable() {
                    public void run() {
                       jProgressBar1.setValue(x);
        SwingUtilities.invokeAndWait(getTextFieldText);
    }The problem i have is that the thread doesn't recognize the gui form. Any help on getting the progress bar to update with the thread would be great! Thanks! :)

    There's been too much bad code submitted to this topic to comment on it individually, but:
    1. Don't call a thread or runnable's run() method directly, if you expect it to be executed in a different thread! Call thread's start method instead.
    2. If you want code executed in the Event Dispatch Thread (EDT), pass a runnable to InvokeLater (usually preferrable to InvokeAndWait). That runnable's run method will be executed in the EDT -- there's no need to write complicated nested run methods!
    In the case of JProgressBar, I was concerned about flooding the EDT with many "invokeLater" runnables, so I penned this wee class:
    import javax.swing.*;
    public class Updater  {
         private final JProgressBar jpb;
         private boolean queued;
         private int val;
         private Runnable runnable = new Runnable() {
              public void run() {//called from EDT:
                   int _val;
                   synchronized(Updater.this) {
                        queued = false;
                        _val = val;
                   jpb.setValue(_val);
         public Updater(JProgressBar jpb) {
              this.jpb = jpb;
         public void update(int _val) {
              boolean go = false;
              synchronized(this) {
                   val = _val;
                   if (!queued) {
                        queued = true;
                        go = true;
              if (go)
                   SwingUtilities.invokeLater(runnable);
    }To use it, you would typically declare it as a field:
    private Updater updater = new Updater(yerBar);Then repeatedly call (from any thread)
    updater.update(yerValue);

  • BB 9650, WIFI in house, did I do this correctly?

    Hi,
    I wanted to use my WIFI as much as I could, I first went into manage connections, and thought I could shut off Mobile network verizon, I did, internet worked, but I did not recieve calls.
    Next try, I have both mobile and wifi connected.
    I went to options, advanced options, browser, and clicked on default browser, and clicked on hotspot browser.
    It seems to work, is this correct?
    My other question is when I leave the wifi area, do I have to change the browser defualt again, or is it smart enought that no wifi, so it uses the network?
    I figure I would use wifi as much as i can for internet, since I have the 150mb plan...
    Would like some input or suggestions
    thanks

    Ok,
    Some of you who remember the 70s TV show "Happy Days" and one of the characters the Fonz?  He was the ultimate cool and had trouble saying the word "Wrong."  Well, fortunately, I am not the Fonz and I can admit when I am wrong.  Or at least part wrong.  Let me correct my previous post in this thread so all will be set straight.
    First, you can use just WiFi on a Blackberry to perform data intensive tasks such as internet browsing, Blackberry Messenger, email, AIM, and those other messaging apps that use data.  However, you will have to turn off the Mobile Network to do so.  You will have to go to Manage Connections and uncheck the Mobile Network box and check the WiFi box.  Obviously you will need a WiFi connection for this task.  I was wrong in that I thought you could turn off the Data from the Mobile connections option under Options and it would leave the 3G signal for calls and such and also leave the Wifi, however, this turns off ALL data.  So, in order to use WiFi only and no 3G signal, you will have to turn off the mobile network all together.  This means no calls or texts can be sent or received.  Now, I have not dabbled into Skype so I do not know how Skype might can figure into this equation for calls. 
    I am sorry for giving out incorrect information, but all is clear now.  It's Wifi and no mobile network and no calls or texts.  Or, turn it on to get calls.  Now, you ask, "Doc, how can I know if my browser is using Mobile Network or Wifi connection?"  Good question.  In your browser, press the Menu key and Options.  Select Browser Configuration.  Change the setting Browser: Hotspot Browser.  Now you will be using WiFi, if it is available.  Also, if you ever want to save a URL into your bookmarks, you can do so by telling it to save as a Hotspot brower URL.  For example, on your device, go to http://community.vzw.com/
    Once the page loads (yes, it is the landing page for these forums, which should show up enhanced to mobile viewing) Press the Menu key and select Add Bookmark.  Name it what you want, I always leave the default.  Scroll down to Browser and if you need to change it to Hotspot Browser, you can do so.  Scroll on down to ADD and it saves it as a Hotspot browser, if WiFi is turned on.  This works for saving any URL. 
    Just a couple of tips I thought I would pass on to help you to understand and, more than any thing, save MONEY.  Again, sorry for posting some inaccurate info.  Should you ever have a question, post it here in the forums.  I am always around to help, as will as some of the other members who have learned just as I have...trial and error.  Good luck.
    Doc

  • I want to install hybrid drive in Macbook pro. I bought my Mac book with lion and am now running mountain lion. I was told I needed install disk to set up new drive before restoring from my time capsule.Is this correct and if so how do I get disk osx10?

    I purchased a Mac book Pro in Aug 2012 with lion and have upgraded to mountain lion. I currently am looking to install the segate 750 gig hybrid drive. I watching video I shows me needing a OSX 10 disk to boot off of when the new disk is in and the use time capsule to restore system from back up drive to new hybrid. Hope I got this correct so far. that being said my Mac Book did not come with a disk therefore is one available, when I get to point of restoring from back up with it be the same mountain lion system I had. Sorr if this sounds redudant.. Thank you in advance

    Connect the new hybrid drive to your MBP.  Start up your MBP holding the OPTION key, the two HDD icons will appear.  Select the 'restore' icon on the right.  When you get to the menu where you have 4 options, select Disk Utility.  Format the drive and then you can use the option of restoring it from Time Machine.
    Ciao.

  • I purchased photoshop elements 8 on line in 2010. I recently switched to a new desktop computer. Photoshop asked for my serial number. I entered the correct serial number (obtained from Adobe). It says it is invalid. How do I get this corrected so I can u

    I purchased photoshop elements 8 on line in 2010. I recently switched to a new desktop computer. Photoshop asked for my serial number. I entered the correct serial number (obtained from Adobe). It says it is invalid. How do I get this corrected so I can use the product I purchased?

    Hi Rosera,
    This is bad and I am sorry to hear that you are running into these issues.
    Let me connect with you, so right now, the current state is that you have installed copy of PSE 8 in trial on your windows 7 machine, and when you are trying to enter the serial number from your PSE 8 Box, it is saying the serial number to be invalid?
    It might sound pretty obvious, but please make sure that you are entering the serial number correctly and of PSE 8(in case you bought a PEPE box)?
    Have you tried the steps mentioned below by Barbara?
    If possible, you can send me the serial number through PRIVATE MESSAGE and I can check it on my end that whether the serial number is indeed invalid or that the issue is with your machine?
    Regards,
    Ankush

  • I erased my 2nd gen ipod touch, can it be restored, apple shop are saying no , some parts can not be downloaded. is this correct

    Sorry new to all this.
    I was re-selling my ipod touch 2nd generation so i erased all contents and setting ... was this correct ?
    The new owner is telling me she cant get it to work. she has taken it into the apple store and they have told her that parts that were erased ca no longer be downloaded.
    Is this correct ?
    Have i been an idiot ? lol

    That sounds correct.
    Many developers when they updated their apps to support iOS 5 and 6 dropped compatibility for iOS 4.2.1 and earlier.
    See the following for more easily  finding compatible apps
    iOSSearch - search the iTunes store for compatible apps.
    Vintapps 3.1.3 - paid app.
    Apple Club - filter apps by iOS version.

  • I have an iphone 5 on contract with 3 but the speaker isn't working , I've spoken with my provider and they said I need to take it back to an apple store .is this correct ? Many thanks

    I have an iphone 5 on contract with 3 but the speaker isn't working , I've spoken with my provider and they said I need to take it back to an apple store .is this correct ? Many thanks

    If you received you phone as part of the contract then surely from a legal pov your contract is with 3 not Apple unless you bought Applecare, if you bought your phone outright then put your 3 SIM into it then yes you'd have to visit an Apple store with your phone's purchase receipt.

  • I read that I can have iworks for free on my iPad if I own a copy for my computer. Is this correct, and how do I download it wit out being charged?

    I read that I can have iworks for free on my iPad if I own a copy for my computer. Is this correct, and how do I download it without being charged?

    There really is nothing more to say than what Allan has already said, but to explain just a little further, what you may have read is that once you purchase an iOS app in the app store, you do not have to purchase it again if you need to download it to another iOS device. In other words once you buy the app, you can load it onto unlimited iPhones, iPod Touches or iPads.
    You must be using the same iTunes account that you originally purchased the app with in order to download it again. But as Allan said, just because you have it on your Mac does not entitle you to download it free to your iPad.

  • Officejet pro 8600 plus power switch light stays on while in sleep mode. is this correct?

    officejet  pro 8600 plus  power switch light stays on while in sleep mode. is this correct?
    This question was solved.
    View Solution.

    The power light will slowly flash on and off when the Officejet Pro 8600 Plus is in a sleep mode state.
    -------------How do I give Kudos? | How do I mark a post as Solved? --------------------------------------------------------
    I am not an HP employee.

  • Changing feed url from Blogger to Podomatic, using Feedburner...Am I doing this correctly?

    Apologies in advance for my newbie-ness, but I recently started a podcast (Candid Voices) using Blogger and then burning with Feedburner, from which I submitted the feed to the Itunes Store (which was accepted).
    However, I have had to switch to PodoMatic for my podcast hosting. I recently updated my Feedburner source url (just an hour ago), and I believe that should cause the Itunes store to update my Podcast eventually...however, I don't want to wait a few days and then figure out there was another step I was supposed to take to complete the changeover.
    Can someone let me know if I've done this correctly and can expect the Store to reflect the changes within a couple days?
    My PodoMatic url: http://candidvoices.podomatic.com
    My Feedburner url: http://feeds.feedburner.com/candidvoiceswithdonny
    If you need other information, please let me know.
    Thanks in advance for your patience and help!

    Oddly you appear to have two identical feeds:
    http://feeds.feedburner.com/candidvoiceswithdonny
    http://feeds.feedburner.com/CandidVoicesWithDonny
    iTunes is using the latter, but they are the same and are both drawn from the Podomatic feed, so I suppose Feedburner is somehow creating two simultaneously. However on your next update you should make sure that the latter feed is indeed being updated.
    Both episodes appear when subscribing, but the Store hasn't caught up yet - it was only posted today, so it should catch up in 1-2 days. There's no further action you need take.

  • I have original ipad and however I cant add my phone number to lmessage it will only take an email address . Is this correct ?

    I have original ipad and however I cant add my phone number to lmessage it will only take an email address . Is this correct ?

    I copied this from an Apple Support article.
    If your phone number is not listed on an iPod touch or iPad, follow these steps:
    Update to the latest version of iOS.
    Verify that you're using the same Apple ID that you are using on your iPhone.
    On your iPad or iPod touch, sign out of your Apple ID in the following locations:
    Settings > Messages > Send & Receive. Tap your Apple ID, then tap Sign Out.
    Settings > FaceTime. Tap your Apple ID, then tap Sign Out.
    Sign in to your Apple ID with FaceTime and iMessage.
    If your phone number is still not listed, follow these steps:
    Sign out of your Apple ID on all iOS devices, including your iPhone, in the following locations:
    Settings > Messages > Send & Receive. Tap your Apple ID, then tap Sign Out.
    Settings > FaceTime. Tap your Apple ID, then tap Sign Out.
    Sign in to your Apple ID on your iPhone.
    When activation is complete, your Apple ID and all verified email addresses should be visible in Settings > Messages > Receive At and also in Settings > FaceTime.
    Now sign in to your Apple ID on your other iOS devices.
    This is the website for the support article.
    http://support.apple.com/kb/HT5538

  • I am having trouble connecting my bluetooth headset on the ipad 1. I have now been told that Apple is only allowing several headsets to be used with Ipad. Is this correct? I have a grundig headset and a plantronics headset but these can both not be found

    I am having trouble connecting my bluetooth headset on the ipad 1. I have now been told that Apple is only allowing several headsets to be used with Ipad. Is this correct? I have a grundig headset and a plantronics headset but these can both not be found

    Check this out....
    http://support.apple.com/kb/HT1664

  • Using 10.6.8, internet pdf documents not opening using Adobe Reader; how is this corrected?

    Using 10.6.8, internet pdf documents not opening using Adobe Reader; how is this corrected?  I've tried changing "sharing and permissions" to "read and write" as suggested on other help sites, but this did not correct the problem.

    I had the same problem---contacted Adobe and their service rep helped me by
    A.   Deleting  Adobe Reader from my iMac's Applications in Finder
    B.   Downloading Google Chrome browser and then
    C.   Downloading the latest Adobe Reader for Mac 10.6.8 in Google Chrome--it works
    Adobe acknowledges problem with the latest Snow Leopard update 10.6.8 and with this work around, I can open PDF's using the Google Chrome Browser.
    Is this the ideal solution---NO.  Safari is my main browser. I now have to use a separate browser for PDF's.
    Am I happy to be able to open PDF's---YES
    You can't believe the time I spent to solve this problem before calling Adobe for help. My Apple Care policy expired a month ago . As I use Photoshop Elements, I was not charged by Adobe for their help on the telephone.

  • The problem with the warranty repair in Russia. I bought an Iphone without a contract in Paris, and now I must go there to have it repaired? In Russia, the repair takes only bought in Russia. Is this correct?

    The problem with the warranty repair in Russia. I bought Iphone4 without a contract in Paris, and now I must go there to have it repaired? In Russia, the repair takes only bought in Russia. Is this correct? So I was told over the phone in service and support in the near future, I was not planning to fly to Paris. Just for the sake of repairing the phone to fly to Paris is expensive.
    The problem of 100% on your phone, as trying to change SIM cards of different operator and the problem remains.
    Phone is constantly losing the signal during a call, and often just disabled.
    I bought 5 phones for himself, his wife and friends, and only I have these problems.
    Very annoying.

    Yes you will need to go back to France as the warranty is country specific except in EU countries.  Or if you know anyone there they could take it to an Apple store if you posted it to them.

  • My iPhone 5 32Gb has a capacity of 28.1Gb is this correct? or bellow par and require replacement?

    Hi, I have purchased a new iPhone 5 32GB, i go into: Settings> About> scroll to capacity and have only 28.1GB.
    I heard i am supose to have 29.8 at least? does my iPhone require replacement and/or is this correct or faulty?
    Mikk

    That is correct. The exact amount of free space depends on the OS version and iPhone configuration, and may vary up or down by a gig or so.
    If there was a problem with the phone memory, you likely would see no storage at all or in fact be unable to even boot it up.

Maybe you are looking for

  • Problem in displaying file thru FDTA

    hi all, we have a problem with FDTA while displaying the content of DME file which generated through FBWE transaction for Bill of Exchanges. it is showing some chinese characters in dispaly. but when we down load the file into PC the file getting dow

  • PO number and Finance doc number connection

    Hi Experts, In our project we got a requirement like we need to show fields from fs10n, fields are, GL account number, FI document number, PO number, Amount in local currency and PO description. I didnt see any datasource which bring PO number, FI GL

  • How and when is the BindingContext collection populated?

    Hello For what I read, a BindingContext is a collection of CurrencyManager objects, and a Form has a BindingContext Property. On the other hand, for each data source on a Form, there is a CurrencyManager object. So, for example, imagine that one have

  • Top-of-page for list display thru ALV

    Hi All, I was suppose to write a detail report thru ALV, this I have handled thru hotspot and in the user_command,I am have the below code.. for ALv I am using FM REUSE_ALV_GRID_DISPLAY FORM User_command. WHEN '&IC1'. PERFORM LIST_DISPLAY using RS_SE

  • Hand gesture for closing apps not working in ios8

    Since updating to ios8 (latest update), the hand gesture for closing apps (3 or 4 fingers) does not work most of the time On my gen 4 iPad. I have to use the 'home' button to close apps and I hate that it now takes two steps to close a web page in Sa