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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:43:08+00:00 2026-05-24T04:43:08+00:00

I am using a JList, and I’m trying to use JTextAreas (that implement ListCellRenderer)

  • 0

I am using a JList, and I’m trying to use JTextAreas (that implement ListCellRenderer) for the cells. It isn’t working. The cells simply display the ListCellRenderer.toString() instead of the actual JTextArea. Could someone help? Thanks.


  DefaultListModel listModel = new DefaultListModel();
  JList list = new JList(listModel);
  add(list);

class ButtonListener implements ActionListener() {
  public void actionPerformed(ActionEvent e){
    listModel.clear();
    for (String s : stringArray) {
      listModel.addElement(new Listm(s));
    }
  }
}

class Listm extends JTextArea implements ListCellRenderer {
  protected Listm(String text) {
   setText(text); //Outputting the text element displays the desired String
  }

  public Component getListCellRendererComponent(JList list, Object object, int number, boolean bool, boolean bool2) {
    setPreferredSize(new Dimension(x, y));
    return this;
    }
  }
}
  • 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-05-24T04:43:09+00:00Added an answer on May 24, 2026 at 4:43 am

    You should post compilable code only, and your code is a bit confusing. You shouldn’t pass text into the renderer’s constructor as this one constructor will be used for the single renderer that renders all items in the list (unless you want all to use the same code). You shouldn’t ignore the Object parameter that is passed into your getListCellRendererComponent method, for this is the data that the renderer item displays. For example:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MyGui extends JPanel {
       public static final String[] DATA = {"One\n1", "Two\n2", "Three\n3"};
       private DefaultListModel listModel = new DefaultListModel();
       private JList list = new JList(listModel);
    
       public MyGui() {
          list.setCellRenderer(new Listm(3, 30));
          add(new JScrollPane(list));
          for (String datum : DATA) {
             listModel.addElement(datum);
          }
       }
    
       private class Listm extends JTextArea implements ListCellRenderer {
          protected Listm(int rows, int cols) {
             super(rows, cols);
             setBorder(BorderFactory.createLineBorder(Color.blue));
          }
    
          public Component getListCellRendererComponent(JList list, Object value,
                   int index, boolean isSelected, boolean cellHasFocus) {
             setText(value.toString());
             if (cellHasFocus) {
                setBackground(FOCUSED_COLOR);
             } else if (isSelected) {
                setBackground(SELECTED_COLOR);
             } else {
                setBackground(null);
             }
             return this;
          }
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("MyGui");
          frame.getContentPane().add(new MyGui());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to display a list of items using DefaultListModel and JList but
My application is using a JList to display numerical values. Here is the default
I'm trying to change a JList inside a JScrollPane dynamically, using myList.setListData(someArray); After this
I'm using a JList as part of a wizard to display all the steps
I'm using custom renderer on JList, but none of components rendered are accessible. list.setCellRenderer(new
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using C#, I need a class called User that has a username, password, active

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.