问题显示JList的组件 [英] Problem displaying components of JList

查看:58
本文介绍了问题显示JList的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Jlist组件中显示Twitter时间轴.我已经使用以下代码修复了框架的大小以及JList的单元格高度和单元格宽度.

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.

例如:假设70宽度正好符合我很好"的鸣叫

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.

我在这里想要做的是,我希望将tweet的其余部分显示在该行下方,因为我确实有足够的高度在第二行中显示tweet.在同一示例中,在单元格中,我希望内容显示为

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

我很好

很棒"

我该如何实现?

推荐答案

正如kleopatra所说,

As kleopatra said,

我们需要一个自定义的ListCellRenderer,它返回一个支持多行文本的组件.默认渲染器返回的JLabel仅是单行.我们需要创建一个这样的渲染器,该渲染器返回一个JTextArea,它可以在其中包裹多行.以下是简单的代码.

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;
   }
}


我们需要添加以下行以将单元格渲染器设置为jlist.


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

jlist.setCellRenderer(new CustomListRenderer());

这篇关于问题显示JList的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆