Reputation: 474
I make a java application, but now I need to display all outputs of the console (like exceptions, println, ecc...) in a external jframe. I viewed some solutions but I don't know what I need to do.
Please don't mark this like a duplicate because I don't find nothing to solve my question.
I tried this :
public class Main extends JFrame {
public static void main(String[] args) {
Console console = new Console();
console.init();
Main launcher = new Main();
launcher.setVisible(true);
console.getFrame().setLocation(
launcher.getX() + launcher.getWidth() + launcher.getInsets().right,
launcher.getY());
}
private Main() {
super();
setSize(600, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class Console {
final JFrame frame = new JFrame();
public Console() {
JTextArea textArea = new JTextArea(24, 80);
textArea.setBackground(Color.BLACK);
textArea.setForeground(Color.LIGHT_GRAY);
textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
textArea.append(String.valueOf((char) b));
}
}));
frame.add(textArea);
}
public void init() {
frame.pack();
frame.setVisible(true);
System.out.println("Hello world!");
}
public JFrame getFrame() {
return frame;
}
}
but it doesn't function.
What I should do? Thanks.
Upvotes: 2
Views: 2313
Reputation: 3467
I fixed your code and improve the solution :
public class Main extends JFrame {
private Main() {
super();
setSize(600, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) throws Exception {
Console console = new Console();
Main launcher = new Main();
launcher.setVisible(true);
console.getFrame().setLocation(
launcher.getX() + launcher.getWidth() + launcher.getInsets().right,
launcher.getY());
System.out.println("1- log");
System.out.println("2- log");
System.out.println("3- log");
System.err.println("1- error");
System.err.println("2- error");
}
}
class Console {
final JFrame frame = new JFrame();
JTextArea textArea;
public Console() throws Exception {
textArea = new JTextArea(24, 80);
textArea.setBackground(Color.BLACK);
textArea.setForeground(Color.LIGHT_GRAY);
textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
frame.add(textArea);
frame.pack();
frame.setVisible(true);
redirectOut();
System.out.println("Hello world!");
System.out.println("test");
// throw new Exception("excccccc");
}
public PrintStream redirectOut() {
OutputStream out = new OutputStream() {
@Override
public void write(int b) throws IOException {
textArea.append(String.valueOf((char) b));
}
};
PrintStream ps = new PrintStream(out);
System.setOut(ps);
System.setErr(ps);
return ps;
}
public JFrame getFrame() {
return frame;
}
}
Upvotes: 1
Reputation: 474
I based on the answer of @Razavi and i modified that to print the errors too :
OutputStream out = new OutputStream() {
@Override
public void write(int b) throws IOException {
textArea.append(String.valueOf((char) b));
}
};
System.setOut(new PrintStream(out));
System.setErr(new PrintStream(out));
Thanks!
Upvotes: 1
Reputation: 505
If you only need to display Strings on the console this will work. Maybe you need to convert more complex data to a String first.
package sof2;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Main2 extends JFrame {
public static void main(String[] args) {
Console console = new Console();
console.init();
Main2 launcher = new Main2();
launcher.setVisible(true);
console.getFrame().setLocation(
launcher.getX() + launcher.getWidth() + launcher.getInsets().right,
launcher.getY());
console.printOnConsole("BLABLABLA");
}
private Main2() {
super();
setSize(600, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class Console {
final JFrame frame = new JFrame();
JTextArea textArea = new JTextArea(24, 80);
public Console() {
textArea.setBackground(Color.BLACK);
textArea.setForeground(Color.LIGHT_GRAY);
textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
textArea.append(String.valueOf((char) b));
}
}));
frame.add(textArea);
}
public void init() {
frame.pack();
frame.setVisible(true);
}
public JFrame getFrame() {
return frame;
}
public void printOnConsole(String s){
this.textArea.append(s);
}
}
I added an function called printOnConsole(String s)
that appends the submitted String to the console.
So in your application e.g you can just call this method with console.printToConsole("I will be displayed in JFrame Console")
This actually only works with Strings but feel free to add more complex data structures.
best regards
Upvotes: 3