Results 1 to 11 of 11
  1. #1
    GuiRitter is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default How to loop forever in Swing-based Java app

    Exactly what it says on the tin. I want to use a while(true) loop for the program to run "forever", but I don't know where in the code to put the loop line. I tried it in the main class' constructor and the window didn't popped up. I tried it in the main method below the contructor call and only the window's borded showed. In both times I had to force-close the application.

    Can you help?

  2. #2
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Create a new thread? ....:eek:

  3. #3
    GuiRitter is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    I have no knowledge of threads yet.

  4. #4
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

  5. #5
    oomrichie is offline Member
    Join Date
    Feb 2011
    Posts
    31
    Rep Power
    0

    Default

    Try this
    Java Code:
            new Thread(new Runnable() {
                    public void run() {
                        while (true) {
                            //Code Goes here
                        }
                    }
                }).start();

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Before doing any of the suggestions above, perhaps you should tell us why you think you need to do this. I have a sneaking suspicion that you're using the wrong tool for your job.

  7. #7
    GuiRitter is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Before doing any of the suggestions above, perhaps you should tell us why you think you need to do this. I have a sneaking suspicion that you're using the wrong tool for your job.
    I wasn't going to reply to this because I actually found out how to do it some time after the first post, just haven't got time to do it yet, but because you asked, I want it to loop forever to poll a game pad. I can even go as far as guessing that you thought I wanted it to work with buttons, so that their state would be constantly read. No, I know how to work with Listeners. But this game pad library doesn't have listeners, so I have to use more primitive ways.

    I am learning Java at my university, and I will learn how to work with threads somewhere in the future, so I want to avoid it now.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by GuiRitter View Post
    I wasn't going to reply to this because I actually found out how to do it some time after the first post, just haven't got time to do it yet, but because you asked, I want it to loop forever to poll a game pad. I can even go as far as guessing that you thought I wanted it to work with buttons, so that their state would be constantly read. No, I know how to work with Listeners. But this game pad library doesn't have listeners, so I have to use more primitive ways.

    I am learning Java at my university, and I will learn how to work with threads somewhere in the future, so I want to avoid it now.
    Yeah, I guess you busted me and my concern. Note thought that you could do your polling with a Swing Timer and not have directly work with threads.

  9. #9
    GuiRitter is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Yeah, I guess you busted me and my concern.
    :D

    Quote Originally Posted by Fubarable View Post
    Note thought that you could do your polling with a Swing Timer and not have directly work with threads.
    Yeah I'm looking into that now. I'll post if I have progress.

    Edit: I'm following a tutorial that shows how to use the Timer class for my application, but I fear that the Java syntax have been updated since the creation of his tutorial (2006). I'll try to post the significant part of the code.

    Java Code:
    public class TestClass extends javax.swing.JFrame {
        public TestClass() {
            initComponents();
            // lots of code in here
            addWindowListener( new WindowAdapter() {
                public void windowClosing(WindowEvent e){
                    pollTimer.cancel(); // was pollTimer.stop(); but NetBeans said "cannot find symbol"; I think this is the same
                    System.exit(0);
                }
            });
            startPolling();
        }
        private void startPolling(){
            ActionListener pollPerformer = new ActionListener() {
                public void actionPerformed(ActionEvent e){
                    // GUI update
                }
            };
            pollTimer = new Timer(DELAY, pollPerformer); // "cannot find symbol"
            pollTimer.start();                           // "cannot find symbol"
        }
        private Timer pollTimer;
        private static final int DELAY = 40;
    }
    Last edited by GuiRitter; 04-28-2011 at 12:44 AM.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    You need a javax.swing.Timer, not a java.util.Timer. Check your imports.

    db

  11. #11
    GuiRitter is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Thanks! That did it! But I must say, Java is quite obscure in this matter. They could've created different named Timer classes so we can know.

    Thanks again for all the help!

Similar Threads

  1. Replies: 2
    Last Post: 01-13-2011, 05:43 AM
  2. Error in running swing based sample application.
    By adityadham in forum SWT / JFace
    Replies: 3
    Last Post: 10-20-2010, 04:24 AM
  3. Error in running swing based sample application.
    By adityadham in forum AWT / Swing
    Replies: 2
    Last Post: 10-19-2010, 04:04 PM
  4. [SOLVED] How to loop forever
    By Manfizy in forum New To Java
    Replies: 18
    Last Post: 06-19-2009, 08:59 AM

Posting Permissions

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