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

  • Home
  • SEARCH
  • 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 9157359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:59:43+00:00 2026-06-17T12:59:43+00:00

I am trying to achieve a simple console with a swing layout that i

  • 0

I am trying to achieve a simple “console” with a swing layout that i will just be able to add simple messages to each one in a different line and different color.

i have been able to do something but i still have problems with it.

i have made 2 attempts at this.

1: with grid layout:

    summeryGrid = new JPanel(new GridLayout(0, 1));
    summeryGrid.setBounds(10, 140, 600, 300);
    summeryGrid.add(createErrorMessage("some error message"));
    summeryGrid.add(createErrorMessage("some error message1"));
    summeryGrid.add(createErrorMessage("some error message2"));

This actualy works fine. but the problem that i am having is that if i don’t have enough messages there is a huge gap between them. i did try to set the hgap and vgap but they had not affect in this matter.

2: attempt is with flow layout. but that damn awful layout seems to work only vertically and not horizontally for some reason. so that’s no good unless i can turn its layout

  • 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-17T12:59:44+00:00Added an answer on June 17, 2026 at 12:59 pm

    You can use a single JEditorPane to display the whole thing.

    Here is a very simple example:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.util.Date;
    import java.util.Random;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.Document;
    import javax.swing.text.MutableAttributeSet;
    import javax.swing.text.SimpleAttributeSet;
    import javax.swing.text.StyleConstants;
    
    public class TestConsole {
    
        private static final String TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec purus sapien, molestie dapibus feugiat vitae, pharetra lobortis lacus. Proin metus neque, malesuada vel consectetur vel, imperdiet et mauris. Vivamus vel tortor ipsum, ac semper ipsum. Nam semper tellus et purus molestie vestibulum. Aliquam erat volutpat. Nam vulputate facilisis magna id sollicitudin. Donec rutrum lorem sit amet orci lacinia congue. Ut nec nibh ipsum, et ornare tellus. Etiam nisi massa, mollis eu viverra id, luctus sed massa. Donec tincidunt erat vel sapien varius ultricies. Vivamus dui diam, consequat nec facilisis ut, interdum at enim. Vestibulum vestibulum, lorem nec cursus eleifend, purus orci egestas quam, vel sodales sem magna at nibh. Cras id nibh eleifend turpis sollicitudin adipiscing. Nunc aliquet posuere vulputate. Suspendisse id augue ut quam mattis sollicitudin. ";
    
        private static final int ROWS = 24;
        private static final int COLUMNS = 80;
    
        private JEditorPane console;
    
        public void append(final String s, final Color color) {
            if (!SwingUtilities.isEventDispatchThread()) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        append(s, color);
                    }
                });
                return;
            }
            Document document = console.getDocument();
            MutableAttributeSet sas = new SimpleAttributeSet();
            StyleConstants.setForeground(sas, color);
            try {
                document.insertString(document.getLength(), s, sas);
            } catch (BadLocationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        private void initUI() {
            JFrame frame = new JFrame(TestConsole.class.getSimpleName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            console = new JEditorPane() {
                @Override
                public Dimension getPreferredSize() {
                    Dimension d = super.getPreferredSize();
                    FontMetrics fm = getFontMetrics(getFont());
                    int colWidth = fm.charWidth('m');
                    int rowHeight = fm.getHeight();
                    d.width = Math.max(d.width, rowHeight * ROWS);
                    d.height = Math.max(d.height, colWidth * COLUMNS);
                    return d;
                }
            };
            console.setEditable(false);
            console.setContentType("text/html");
            console.setForeground(Color.WHITE);
            console.setBackground(Color.BLACK);
            console.setFont(new Font("Consolas", Font.PLAIN, 14));
            frame.add(new JScrollPane(console));
            frame.pack();
            frame.setVisible(true);
            Timer t = new Timer();
            t.scheduleAtFixedRate(new TimerTask() {
                Random random = new Random();
    
                @Override
                public void run() {
                    int start = random.nextInt(TEXT.length());
                    int size = random.nextInt(TEXT.length() - start);
                    append(TEXT.substring(start, start + size) + "\n", new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
                }
            }, new Date(), 1000);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new TestConsole().initUI();
                }
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I'm trying to achieve must be simple but I just can't get it
I'm very new to Redmine/Ruby trying to achieve a simple plugin that takes the
I'm trying to achieve something that seems quite simple but I have hard time
I'm trying to achieve a simple layout of 4 horizontal inline-blocks within a container.
What I'm trying to achieve is simple. I just don't know the jQuery lingo'
I'm trying to achieve a very basic goal that used to be quite simple
I am trying to achieve a very simple goal, however it does not seem
Trying to achieve any moving effect while appending an element from one to another
I am trying to achieve a simple UI with the following design: But am
Its pretty simple what im trying to achieve. I have a MC on Stage,

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.