Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to visualize console java in JFrame/JPanel

I made a Java program using Swing libraries. Now I would like to redirect my console outputs into a JFrame or JPanel.

like image 399
Gianfra Avatar asked Mar 19 '12 19:03

Gianfra


People also ask

How to create jpanel in Java?

How to Create Panel in Java? As already discussed, JPanel class is of the package java.swing and subclass of java.swing.JComponent. 1. JPanel object is created as mentioned below. JPanel jp=new JPanel (); //jp is the object. 2. Create a class that extends from JPanel Class. public class jpclass extends JPanel () {.

How to redirect console output to JFrame or jpanel?

Now I would like to redirect my console outputs into a JFrame or JPanel. Show activity on this post. You need to make an OutputStream that re-directs output to the text area and that implements all the necessary methods of the OutputStream interface, and then in your main program, redirect your Standard output into this stream.

How to append text to a JFrame or jpanel?

Once you have your JFrame or JPanel, add a text field to it. JTextArea is a good choice, because it's multiple lines. Once it is added, you can .append ('text'); to it instead of writing the System.out.print (); JFrame jFrame = new JFrame (); JTextArea jTextArea = new JTextArea (); jTextArea.append ( "Hello World." ); jFrame.add ( jTextArea );

What is the use of it class in jpanel?

It inherits the JComponents class. It doesn't have title bar. It is used to create a new JPanel with a double buffer and a flow layout. It is used to create a new JPanel with FlowLayout and the specified buffering strategy. It is used to create a new JPanel with the specified layout manager.


2 Answers

I know I'm late, but for someone that is looking for a better, more compact answer i have made this witch combines two custom classes.

Console.java

public class Console {
    final JFrame frame = new JFrame("CONSOLE");
    ColorPane textPane;
    JScrollPane scroll;

    public Console() throws Exception {
        textPane = new ColorPane();
        textPane.setPreferredSize(new Dimension(525, 600));
        textPane.setFont(new Font("Lucida Console", Font.BOLD, 12));
        scroll = new JScrollPane(textPane);
        textPane.setBackground(Color.BLACK);

        scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );

        textPane.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                scrollToBottom(scroll);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {

            }

            @Override
            public void changedUpdate(DocumentEvent e) {

            }
        });

        //Add textPane in to middle panel
        frame.add(scroll);
        frame.setSize(100, 600);
        frame.pack();
        frame.setVisible(false);
        redirectOut();

    }

    public PrintStream redirectOut() {
        OutputStream out = new OutputStream() {
            @Override
            public void write(int b) throws IOException {
                textPane.append(Color.WHITE, String.valueOf((char) b));
            }
        };
        OutputStream errOut = new OutputStream() {
            @Override
            public void write(int b) throws IOException {
                textPane.append(Color.RED, String.valueOf((char) b));
            }
        };
        PrintStream ps = new PrintStream(out);
        PrintStream err = new PrintStream(errOut);

        System.setOut(ps);
        System.setErr(err);
        scrollToBottom(scroll);

        return ps;
    }

    public JFrame getFrame() {
        return frame;
    }

    private void scrollToBottom(JScrollPane scrollPane) {
        JScrollBar verticalBar = scrollPane.getVerticalScrollBar();
        AdjustmentListener downScroller = new AdjustmentListener() {
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                Adjustable adjustable = e.getAdjustable();
                adjustable.setValue(adjustable.getMaximum());
                verticalBar.removeAdjustmentListener(this);
            }
        };
        verticalBar.addAdjustmentListener(downScroller);
    }
}

and ColorPane.java

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class ColorPane extends JTextPane {

    public void appendNaive(Color c, String s) { // naive implementation
        // bad: instiantiates a new AttributeSet object on each call
        SimpleAttributeSet aset = new SimpleAttributeSet();
        StyleConstants.setForeground(aset, c);

        int len = getText().length();
        setCaretPosition(len); // place caret at the end (with no selection)
        setCharacterAttributes(aset, false);
        replaceSelection(s); // there is no selection, so inserts at caret
    }

    public void append(Color c, String s) { // better implementation--uses
        // StyleContext
        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
                StyleConstants.Foreground, c);

        int len = getDocument().getLength(); // same value as
        // getText().length();
        setCaretPosition(len); // place caret at the end (with no selection)
        setCharacterAttributes(aset, false);
        replaceSelection(s); // there is no selection, so inserts at caret
    }

    public static boolean isPrime(int n) {
        if (n < 2)
            return false;
        double max = Math.sqrt(n);
        for (int j = 2; j <= max; j += 1)
            if (n % j == 0)
                return false; // j is a factor
        return true;
    }

    public static boolean isPerfectSquare(int n) {
        int j = 1;
        while (j * j < n && j * j > 0)
            j += 1;
        return (j * j == n);
    }

}

This is the solution that I have been using and it even has colored output. PS: btw, sry for bad english (i'm only 11)

like image 60
victorgamer235 Avatar answered Sep 21 '22 00:09

victorgamer235


You need to make an OutputStream that re-directs output to the text area and that implements all the necessary methods of the OutputStream interface, and then in your main program, redirect your Standard output into this stream. I've used something like this for one of my programs:

import java.io.IOException;
import java.io.OutputStream;

import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class TextAreaOutputStream extends OutputStream {

   private final JTextArea textArea;
   private final StringBuilder sb = new StringBuilder();
   private String title;

   public TextAreaOutputStream(final JTextArea textArea, String title) {
      this.textArea = textArea;
      this.title = title;
      sb.append(title + "> ");
   }

   @Override
   public void flush() {
   }

   @Override
   public void close() {
   }

   @Override
   public void write(int b) throws IOException {

      if (b == '\r')
         return;

      if (b == '\n') {
         final String text = sb.toString() + "\n";
         SwingUtilities.invokeLater(new Runnable() {
            public void run() {
               textArea.append(text);
            }
         });
         sb.setLength(0);
         sb.append(title + "> ");
         return;
      }

      sb.append((char) b);
   }
}

And you can demonstrate it with this:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import javax.swing.*;

@SuppressWarnings("serial")
public class TextAreaOutputStreamTest extends JPanel {

   private JTextArea textArea = new JTextArea(15, 30);
   private TextAreaOutputStream taOutputStream = new TextAreaOutputStream(
         textArea, "Test");

   public TextAreaOutputStreamTest() {
      setLayout(new BorderLayout());
      add(new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
      System.setOut(new PrintStream(taOutputStream));

      int timerDelay = 1000;
      new Timer(timerDelay , new ActionListener() {
         int count = 0;
         @Override
         public void actionPerformed(ActionEvent arg0) {

            // though this outputs via System.out.println, it actually displays
            // in the JTextArea:
            System.out.println("Count is now: " + count + " seconds");
            count++;
         }
      }).start();
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("Test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new TextAreaOutputStreamTest());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
like image 45
Hovercraft Full Of Eels Avatar answered Sep 22 '22 00:09

Hovercraft Full Of Eels