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 8127879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:41:20+00:00 2026-06-06T07:41:20+00:00

I have this JList that I’m using as the display for System.out . I’m

  • 0

I have this JList that I’m using as the display for System.out. I’m creating an IRC client with this JList at the bottom of the JFrame. It seems that whenever 2 or more elements are added to the JLists ListModel simultaneously, the whole JList becomes white and non-interactive until one single element is added by itself. An example of when multiple elements would be added simultaneously is when an exception prints the stack trace.

Here is the OutputStream class I have that is set as System.out…

public class LogOutputStream extends OutputStream {
    String sentence = "";
    boolean writing = false;
    private DefaultListModel subject;

    public LogOutputStream(DefaultListModel logListModel) {
        this.subject = logListModel;
    }

    public void write(final int b) {
        try {
            sentence = sentence + (char) b;

            if (sentence.endsWith("\n") && !sentence.isEmpty() && !sentence.equals("\t") && !sentence.equals("\r\n") && !sentence.equals("\n")) {
                if (writing == false) {
                    writing = true;
                    subject.addElement(sentence.replaceAll("\n", "").replaceAll("\r", "").trim());
                }

                sentence = "";
                writing = false;
            }
        } 
        catch (Exception e) {
            e.printStackTrace(Boot.stdErr);
        }
    }
}

Does anyone know what’s going on here, and how it can be fixed?

  • 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-06T07:41:22+00:00Added an answer on June 6, 2026 at 7:41 am

    It is likely a Swing concurrency issue in that you’re adding items to the JList’s model off of the event thread. This can tie up the thread and prevent it from doing its necessary actions of drawing the GUI or allowing for user interaction effectively freezing your Swing GUI.

    Try making sure that you only write to the JList on the EDT (the Swing Event Dispatch Thread). Something like this will queue the addElement(...) call on the Swing event thread:

    public void write(final int b) {
        try {
            sentence = sentence + (char) b;
            if (sentence.endsWith("\n") && !sentence.isEmpty()
                    && !sentence.equals("\t") && !sentence.equals("\r\n")
                    && !sentence.equals("\n")) {
                if (writing == false) {
                    writing = true;
                    final String text = sentence.replaceAll("\n", "")
                                    .replaceAll("\r", "").trim();
                    javax.swing.SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            subject.addElement(text);
                        }
                    });
    
                }
                sentence = "";
                writing = false;
            }
        } catch (Exception e) {
            e.printStackTrace(Boot.stdErr);
        }
    }
    

    Edit 1
    This is closer to something I’ve done in the past, using a StringBuilder:

    import java.io.IOException;
    import java.io.OutputStream;
    
    import javax.swing.DefaultListModel;
    import javax.swing.SwingUtilities;
    
    public class JListOutputStream extends OutputStream {
       private DefaultListModel logListModel;
       private StringBuilder sb = new StringBuilder();   
    
       public JListOutputStream(DefaultListModel logListModel) {
          this.logListModel = logListModel;
       }
    
       @Override
       public void write(int b) throws IOException {
          if (b == '\r')
             return;
    
          if (b == '\n') {
             final String text = sb.toString().trim();
             sb.setLength(0);
             if (text.isEmpty()) {
                return;
             }
             SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                   logListModel.addElement(text);
                }
             });
    
             return;
          }
    
          sb.append((char) b);
       }
    
    }
    

    Edit 2
    As per this article titled Writing Your Own Java I/O Stream Classes, if you’re extending OutputStream, don’t forget to provide custom implementations for the flush() and close() methods.

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

Sidebar

Related Questions

I have this method here that gets an object from a JList. The object
I have this JList on my JFrom that keeps expanding both horizontally and vertically
I have a JDialog dlg, created by a JFrame frm, that contains a JList
We have this software that has a webservices component. Now, the administrator of this
I have this membership site setup on my local machine using the ASP.NET membership
I have this DependencyProperty which holds an entity with a property that is a
I have this batch that needs to run that the user has to execute
I'm having this problem: I have a JList (within a JScrollPane) with say about
I have a JList with items that I want to show two values. Is
I'm trying to change a JList inside a JScrollPane dynamically, using myList.setListData(someArray); After this

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.