how to create a console in java gui

Solutions on MaxInterview for how to create a console in java gui by the best coders in the world

showing results for - "how to create a console in java gui"
Pia
01 Jun 2016
1try(Scanner scan=new Scanner(System.in)){
2	//read from console
3	String textRead=scan.nextLine();
4	//write to console
5  	System.out.println(textRead);
6}
Francesca
31 Apr 2017
1import java.awt.*;
2import java.io.*;
3import java.util.*;
4import java.util.List;
5import javax.swing.*;
6
7public class TextAreaOutputStream
8extends OutputStream
9{
10
11// *************************************************************************************************
12// INSTANCE MEMBERS
13// *************************************************************************************************
14
15private byte[]                          oneByte;                                                    // array for write(int val);
16private Appender                        appender;                                                   // most recent action
17
18public TextAreaOutputStream(JTextArea txtara) {
19    this(txtara,1000);
20    }
21
22public TextAreaOutputStream(JTextArea txtara, int maxlin) {
23    if(maxlin<1) { throw new IllegalArgumentException("TextAreaOutputStream maximum lines must be positive (value="+maxlin+")"); }
24    oneByte=new byte[1];
25    appender=new Appender(txtara,maxlin);
26    }
27
28/** Clear the current console text area. */
29public synchronized void clear() {
30    if(appender!=null) { appender.clear(); }
31    }
32
33public synchronized void close() {
34    appender=null;
35    }
36
37public synchronized void flush() {
38    }
39
40public synchronized void write(int val) {
41    oneByte[0]=(byte)val;
42    write(oneByte,0,1);
43    }
44
45public synchronized void write(byte[] ba) {
46    write(ba,0,ba.length);
47    }
48
49public synchronized void write(byte[] ba,int str,int len) {
50    if(appender!=null) { appender.append(bytesToString(ba,str,len)); }
51    }
52
53@edu.umd.cs.findbugs.annotations.SuppressWarnings("DM_DEFAULT_ENCODING")
54static private String bytesToString(byte[] ba, int str, int len) {
55    try { return new String(ba,str,len,"UTF-8"); } catch(UnsupportedEncodingException thr) { return new String(ba,str,len); } // all JVMs are required to support UTF-8
56    }
57
58// *************************************************************************************************
59// STATIC MEMBERS
60// *************************************************************************************************
61
62    static class Appender
63    implements Runnable
64    {
65    private final JTextArea             textArea;
66    private final int                   maxLines;                                                   // maximum lines allowed in text area
67    private final LinkedList<Integer>   lengths;                                                    // length of lines within text area
68    private final List<String>          values;                                                     // values waiting to be appended
69
70    private int                         curLength;                                                  // length of current line
71    private boolean                     clear;
72    private boolean                     queue;
73
74    Appender(JTextArea txtara, int maxlin) {
75        textArea =txtara;
76        maxLines =maxlin;
77        lengths  =new LinkedList<Integer>();
78        values   =new ArrayList<String>();
79
80        curLength=0;
81        clear    =false;
82        queue    =true;
83        }
84
85    synchronized void append(String val) {
86        values.add(val);
87        if(queue) { queue=false; EventQueue.invokeLater(this); }
88        }
89
90    synchronized void clear() {
91        clear=true;
92        curLength=0;
93        lengths.clear();
94        values.clear();
95        if(queue) { queue=false; EventQueue.invokeLater(this); }
96        }
97
98    // MUST BE THE ONLY METHOD THAT TOUCHES textArea!
99    public synchronized void run() {
100        if(clear) { textArea.setText(""); }
101        for(String val: values) {
102            curLength+=val.length();
103            if(val.endsWith(EOL1) || val.endsWith(EOL2)) {
104                if(lengths.size()>=maxLines) { textArea.replaceRange("",0,lengths.removeFirst()); }
105                lengths.addLast(curLength);
106                curLength=0;
107                }
108            textArea.append(val);
109            }
110        values.clear();
111        clear =false;
112        queue =true;
113        }
114
115    static private final String         EOL1="\n";
116    static private final String         EOL2=System.getProperty("line.separator",EOL1);
117    }
118
119} /* END PUBLIC CLASS */
120
Alessandro
16 Aug 2018
1# comile Java class
2javac YourClass.java
3# run compiled Java class
4java YourClass
5# run Java class directly
6java YourClass.java
7# run JAR
8java -jar YourJar.jar