Results 1 to 8 of 8
  1. #1
    DFTBA is offline Member
    Join Date
    Jun 2012
    Posts
    12
    Rep Power
    0

    Default Press key to exit loop

    In my program, I am constantly performing the same task, as many times per second as possible. The thing is, I want to be able to exit the loop at will. I think the best way is to have it make the user press the "Q" key to end the program. How do I set this up? I know I need to use break and keyevent. But I'm not sure how to implement it.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Press key to exit loop

    Unanswerable without more detail.

    If this is a console program, it can't be done. Not with a single keypress anyways.

    If this is a GUI program you need to tell us what GUI toolkit is used.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DFTBA is offline Member
    Join Date
    Jun 2012
    Posts
    12
    Rep Power
    0

    Default Re: Press key to exit loop

    I'm using AWT and Swing. I think that answers your question.

  4. #4
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: Press key to exit loop

    Then your best bet is to put the executing code into a thread (a Runnable class or Thread class), make it check some variable for quit condition (i.e. boolean isTimeToQuit) then stick a KeyListener (How to Write a Key Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)) on your window to catch the 'Q' press and change isTimeToQuit to true.

  5. #5
    DFTBA is offline Member
    Join Date
    Jun 2012
    Posts
    12
    Rep Power
    0

    Default Re: Press key to exit loop

    I don't know what that means. Runnable class? Thread class? And I don't know how the KeyListener is implemented. Can I get more detail?

  6. #6
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: Press key to exit loop

    I left a link for the KeyListener, as for the Thread and Runnable they are two ways to split your program into two threads and make it do more than one thing simultaneously, it's easy to find info on them online. Just look up multi-threaded programming in Java

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

    Default Re: Press key to exit loop

    Quote Originally Posted by DFTBA View Post
    I don't know what that means. Runnable class? Thread class? And I don't know how the KeyListener is implemented. Can I get more detail?
    Surely Google is working in your part of the world?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Press key to exit loop

    Quote Originally Posted by Fubarable View Post
    Surely Google is working in your part of the world?
    Google *what*?

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Press any key to continue
    By romero4742 in forum New To Java
    Replies: 3
    Last Post: 05-23-2012, 05:20 AM
  2. Replies: 10
    Last Post: 05-11-2011, 03:26 AM
  3. Replies: 5
    Last Post: 02-11-2011, 09:33 PM
  4. How to exit the program by typing "exit"?
    By Laythe in forum New To Java
    Replies: 6
    Last Post: 08-19-2009, 09:32 PM
  5. Press any key to continue/press enter
    By JT4NK3D in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 10:27 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
  •