Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Console box with scanner?

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Question Console box with scanner?

    So I found this really cool code here:
    http://www.exampledepot.com/egs/java...#comment-50625

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
     
    public class Console extends JFrame {
        PipedInputStream piOut;
        PipedInputStream piErr;
        PipedOutputStream poOut;
        PipedOutputStream poErr;
        JTextArea textArea = new JTextArea();
     
        public Console() throws IOException {
            // Set up System.out
            piOut = new PipedInputStream();
            poOut = new PipedOutputStream(piOut);
            System.setOut(new PrintStream(poOut, true));
     
            // Set up System.err
            piErr = new PipedInputStream();
            poErr = new PipedOutputStream(piErr);
            System.setErr(new PrintStream(poErr, true));
     
            // Add a scrolling text area
            textArea.setEditable(false);
            textArea.setRows(20);
            textArea.setColumns(50);
            getContentPane().add(new JScrollPane(textArea), BorderLayout.CENTER);
            pack();
            setVisible(true);
     
            // Create reader threads
            new ReaderThread(piOut).start();
            new ReaderThread(piErr).start();
        }
     
        class ReaderThread extends Thread {
            PipedInputStream pi;
     
            ReaderThread(PipedInputStream pi) {
                this.pi = pi;
            }
     
            public void run() {
                final byte[] buf = new byte[1024];
                try {
                    while (true) {
                        final int len = pi.read(buf);
                        if (len == -1) {
                            break;
                        }
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                textArea.append(new String(buf, 0, len));
     
                                // Make sure the last line is always visible
                                textArea.setCaretPosition(textArea.getDocument().getLength());
     
                                // Keep the text area down to a certain character size
                                int idealSize = 1000;
                                int maxExcess = 500;
                                int excess = textArea.getDocument().getLength() - idealSize;
                                if (excess >= maxExcess) {
                                    textArea.replaceRange("", 0, excess);
                                }
                            }
                        });
                    }
                } catch (IOException e) {
                }
            }
        }
    }

    This code basically makes my program display in a console box from what I can tell. I was wondering if anyone knows how to make it so it has a input like scanner too?
    Maybe its too much to bother you guys for, but I really want to use this and I can't do it myself since I hardly understand what its doing heh..

    But seriously any help would be awesome.


  2. #2
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Console box with scanner?

    Maybe dialog boxes are my only solution?

    ew dialog boxes are soo ugly. I'm going to have to actually learn how to make a decent gui.
    Last edited by Hallowed; May 26th, 2011 at 01:13 AM.

Similar Threads

  1. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  2. Clearing text from the console
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 5
    Last Post: December 3rd, 2010, 04:36 AM
  3. How do i run this Accounting console app?
    By mirzahat in forum AWT / Java Swing
    Replies: 2
    Last Post: November 15th, 2010, 11:22 PM
  4. Console Application - How to run it?
    By mirzahat in forum AWT / Java Swing
    Replies: 3
    Last Post: November 15th, 2010, 11:21 PM
  5. [SOLVED] help with console input strnig....
    By mdstrauss in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: August 17th, 2009, 03:59 AM