Help with java listeners or something similiar

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ayebaleet
    New Member
    • Jul 2008
    • 2

    Help with java listeners or something similiar

    Hello everybody,

    I understand the basics of Java, but not such features as are helpful in an applet namely Listeners.

    I wanted to create a feature like this:

    A program would be displayed in the regular old System.out.
    It would be looping continuously doing something unimportant, say counting how many times it could count to a billion, until the user enters a command, and then it would simply delay what it was doing, display something unimportant ("hello") and then go back to what it was doing.

    At current I only know how to make the program Stop and Wait for the user to enter something, and then do it again.

    I know that a key listener or something similar is what I'm after, but I don't know how to use them or if I need one, because I only want input from System.in (which I would usually use a Scanner to get).

    Could someone point me to a proper tutorial, or perhaps crack out an example for me?

    All in all I'm looking for:

    Something that will listen for the user to hit the Enter key, then read in whatever they typed before it in the command line, rather than some newly created and displayed text box.

    Thanks everyone for your time.
  • ajos
    Contributor
    • Aug 2007
    • 283

    #2
    Hold on. I didnt go beyond the first line, you really need to learn the language first before doing anything.

    Start here Sun Tutorials

    regards,

    ajos

    Comment

    • Ayebaleet
      New Member
      • Jul 2008
      • 2

      #3
      Thanks, I used the wrong wording.
      I know Java well enough to create a text only spreadsheet application displayed in the cmd window. I know queues, stacks, binary search trees, how to create iterators and all that good stuff used for ordering and manipulating info. I just don't know app development or how to get input without halting everything.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        OK, I'd say you should use threads (one constantly counting to a billion and one waiting for user input). Search google to find loads of tutorials and examples for thread programming.

        As far as I know (but there's still a lot to learn about Java, so I could be wrong), listeners as known by Java, are only for GUIs, not for the command line (and therefore System.in).

        Greetings,
        Nepomuk

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          I believe you're correct about Listeners being GUI only. Threads are probably the way to go here...

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Laharl
            I believe you're correct about Listeners being GUI only. Threads are probably the way to go here...
            Listeners are a general purpose solution; they're not GUI only. Have a look
            at the PropertyChangeS upport class for example: any bound property
            can use it. And should use it most of the time to decouple parts of groups of
            classes from eachother.

            kind regards,

            Jos

            Comment

            Working...