Results 1 to 3 of 3

Thread: Console Input Without The Return

  1. #1

    Thread Starter
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158

    Console Input Without The Return

    Hi,
    Does anyone know a way I can get input from the console without having the user hit return.

    Say for example I display a menu, like so:

    ------------------------------------------------------------------------------------

    [ MAIN MENU ]

    [1] Create new account
    [2] CLose account
    [3] Display all accounts
    [4] Display selected account

    [5] Exit

    Awaiting Selection:

    ------------------------------------------------------------------------------------

    now say the user hits the number 1 key, I want the program to know he's/she selected the first opention as soon as he presses that key, without having to type 1 then hit return etc.

    Oh one more thing does anyone know how to change text colour under the console and place text at given cordinates ?

    [b] THANKS IN ADVANCE [/]

  2. #2
    VirtuallyVB
    Guest
    I'd recommend javax.swing.JTextArea or java.awt.TextArea. From the docs:
    The java.awt.TextArea could be monitored for changes by adding a TextListener for TextEvent's. In the JTextComponent based components, changes are broadcasted from the model via a DocumentEvent to DocumentListeners. The DocumentEvent gives the location of the change and the kind of change if desired. The code fragment might look something like:


    DocumentListener myListener = ??;
    JTextArea myArea = ??;
    myArea.getDocument().addDocumentListener(myListener);
    Then monitor the fact that text has changed (before even a carriage return). In effect, you are creating your own "console" by this technique.

    Oh, regarding color and coordinates. I came across something that allowed html constructs on a button (and I think other objects with text features--such as JTextArea). Maybe that can help.

    You can also open streams on a (real) "DOS console" window that you launch as a new process (check Process and Runtime.exec) to handle data before a CR, but I don't know about the interaction with a user--so this streaming technique may not work for your needs. This may be analogous to trying to override a "final" method of the object "DOS Console"; the DOS console has its own "interface".

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Oh one more thing does anyone know how to change text colour under the console and place text at given cordinates ?
    I dont know about the console but as VirtuallyVB pointed out you could use a javax.swing.JText Area and change the font or the forground color if you want.
    Code:
    import javax.swing.*; 
      import java.awt.*; 
    
      class JTAExample{
       public static void main(String[] args){
        JComponent jc; 
        jc = changeComponentStates(new JTextArea());
             addComponents(new JFrame("JTAExample"), jc);  
       }
       public static JComponent changeComponentStates(JComponent jc){
         jc.setBackground(Color.green); // setBackground() method part of the Component class
        // jc.setWrapStyleWord(true); // not allowed, jc refrences a JComponent not a JTextArea!
        
         jc.setFont(new Font("Informal Roman", Font.BOLD, 44)); 
         return jc;
       }
       
        public static void addComponents(JFrame jf, JComponent jc){
          Container c = jf.getContentPane();
          c.add(jc);
          jf.addWindowListener(new WindowMonitor()); 
          jf.setSize(400,300);
          jf.setVisible(true);  
       }
     }

Posting Permissions

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



Click Here to Expand Forum to Full Width