Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8689693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:43:00+00:00 2026-06-12T23:43:00+00:00

I’m trying to create a GUI Panel for a program and I’d like everything,

  • 0

I’m trying to create a GUI Panel for a program and I’d like everything, that would normally print to my command prompt, to print to a TextArea object. I have the GUI panel formatted for the most part, I can’t get the text to print to the TextArea though, here’s my file:

package guipanel;

import javax.swing.*;
import java.awt.*;
import java.io.*;


/**
 *
 * @author Dan
 */
public class GUIPanel extends JFrame { 
    public GUIPanel() {
        initComponents();
    }
    private void setOutputStream(boolean catchErrors) {
        System.setOut(aPrintStream); 
        setVisible(true);
        requestFocus();
        if (catchErrors) {
               System.setErr(aPrintStream);
        }
    }
    private void addTabs(JTabbedPane jTabbedPane1) {
        JPanel jPanel1 = new JPanel();
        JPanel jPanel2 = new JPanel();
        JPanel jPanel3 = new JPanel();
        JPanel jPanel4 = new JPanel();
        jTabbedPane1.add("Main", textArea1);
        jTabbedPane1.add("Commands", jPanel);
        jTabbedPane1.add("Rules", jPanel1);
        jTabbedPane1.add("Links", jPanel2);
        jTabbedPane1.add("Information", jPanel3);
        jTabbedPane1.add("Shutdown", jPanel4);
        setOutputStream(true);
    }
    @SuppressWarnings("unchecked")
    private void initComponents() {

        textArea1 = new java.awt.TextArea();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        textArea1.setPreferredSize(new java.awt.Dimension(432, 343));
        textArea1.getAccessibleContext().setAccessibleParent(jTabbedPane1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Evolution-X 639");
        setBounds(new java.awt.Rectangle(0, 0, 400, 450));
        setResizable(false);
        getContentPane().setLayout(new java.awt.FlowLayout());

        addTabs(jTabbedPane1);
        getContentPane().add(jTabbedPane1);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIPanel().setVisible(true);
            }
        });
    }
    private JMenu jMenu1;
    private JMenu jMenu2;
    private JMenuBar jMenuBar1;
    private JTabbedPane jTabbedPane1;
    private TextArea textArea1;
    private JPanel jPanel = new JPanel();
    private PrintStream aPrintStream  =
       new PrintStream(
         new FilterOutputStream(
           new ByteArrayOutputStream()));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:43:02+00:00Added an answer on June 12, 2026 at 11:43 pm

    You need to redirect the print stream to an output stream you can control…

    This is an example of concept I developed for an application I’m working on for work. We use this to bring up the output console when it’s running at user sites so we can see what’s being sent to standard out…until we fixed our logging that is 😉

    Basically it puts a custom OutputStream in between the print stream and the console to capture the output, but still allows the content to printed to the console. This is helpful if you’re running the program from the command line or IDE. You could put a switch to stop this if you wanted…

    enter image description here

    public class TestRedirect {
    
        public static void main(String[] args) {
            new TestRedirect();
        }
    
        public TestRedirect() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    CapturePane capturePane = new CapturePane();
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(capturePane);
                    frame.setSize(200, 200);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
                    PrintStream ps = System.out;
                    System.setOut(new PrintStream(new StreamCapturer("STDOUT", capturePane, ps)));
    
                    System.out.println("Hello, this is a test");
                    System.out.println("Wave if you can see me");
                }            
            });
        }
    
        public class CapturePane extends JPanel implements Consumer {
    
            private JTextArea output;
    
            public CapturePane() {
                setLayout(new BorderLayout());
                output = new JTextArea();
                add(new JScrollPane(output));
            }
    
            @Override
            public void appendText(final String text) {
                if (EventQueue.isDispatchThread()) {
                    output.append(text);
                    output.setCaretPosition(output.getText().length());
                } else {
    
                    EventQueue.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            appendText(text);
                        }
                    });
    
                }
            }        
        }
    
        public interface Consumer {        
            public void appendText(String text);        
        }
    
        public class StreamCapturer extends OutputStream {
    
            private StringBuilder buffer;
            private String prefix;
            private Consumer consumer;
            private PrintStream old;
    
            public StreamCapturer(String prefix, Consumer consumer, PrintStream old) {
                this.prefix = prefix;
                buffer = new StringBuilder(128);
                buffer.append("[").append(prefix).append("] ");
                this.old = old;
                this.consumer = consumer;
            }
    
            @Override
            public void write(int b) throws IOException {
                char c = (char) b;
                String value = Character.toString(c);
                buffer.append(value);
                if (value.equals("\n")) {
                    consumer.appendText(buffer.toString());
                    buffer.delete(0, buffer.length());
                    buffer.append("[").append(prefix).append("] ");
                }
                old.print(c);
            }        
        }    
    }
    

    enter image description here
    enter image description here

    Updated with working example. Test on Windows 7, Java 6 and Mac OS Lion Java 7

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.