Problem displaying components of JList
Asked Answered
C

3

2

I am trying to dsiplay the twitter timeline in a Jlist component. I have already fixed the size of my frame and the cell height and cell width of the JList too with the following code.

jlist.setFixedCellHeight(50);
jlist.setFixedCellWidth(70);

I find that the height and width of each cell are fine but if the content of the tweet inside the cell exceeds the width it is not displaying the further part of the tweet.

For example: Assume that 70 width exactly fits the tweet "I am good"

Suppose if the tweet is "I am good and great" The tweet is getting displayed as "I am good....." The exceeded part of the tweet is not getting displayed.

What I want to do here is, I want the rest out part of the tweet to be displayed below the line as I do have sufficient height to display the tweet in a second line. In the same example, within the cell, I want the content to be displayed as

"I am good

and great"

How can I achieve this?

Cystoscope answered 12/4, 2011 at 6:9 Comment(0)
C
2

As kleopatra said,

We need a custom ListCellRenderer which returns a component that supports multiple lines of text. The default renderer returns a JLabel with is single-line only. We need to create such a renderer which returns a JTextArea that can wrap multiple lines within it. The following is the simple code for this.

public class CustomListRenderer implements ListCellRenderer {

   @Override
   public Component getListCellRendererComponent(JList list, Object value, int index,
        boolean isSelected, boolean cellHasFocus) {

        JTextArea renderer = new JTextArea(3,10);
        renderer.setText(value.toString());
        renderer.setLineWrap(true);
        return renderer;
   }
}

We need to add the following line to set the cell renderer to the jlist.

jlist.setCellRenderer(new CustomListRenderer());
Cystoscope answered 13/4, 2011 at 6:29 Comment(0)
A
1

you need a custom ListCellRenderer which returns a component that supports multiple lines of text. The default renderer returns a JLabel with is single-line only. You can implement f.i a renderer which returns a JTextArea

Athalla answered 12/4, 2011 at 8:43 Comment(2)
Thanks Kleopatra. can you please provide me a code snippet for the same?Cystoscope
what did you try and where exactly is the problem? Anyway, here's an example of a fairly complex renderer weblogs.java.net/blog/zixle/archive/2006/12/…Athalla
E
0

quoting from the JList java doc

JList doesn't implement scrolling directly. To create a list that scrolls, make it the viewport view of a JScrollPane. For example:

JScrollPane scrollPane = new JScrollPane(myList);

// Or in two steps: JScrollPane scrollPane = new JScrollPane(); scrollPane.getViewport().setView(myList);

Electrode answered 12/4, 2011 at 8:5 Comment(1)
I am afraid you got me wrong. I do not want the scroll functionality for a Jlist. I need want the cell content to be displayed in multiple lines within same cell instead of displaying it in a single line!!!!Cystoscope

© 2022 - 2024 — McMap. All rights reserved.