具有一组对象的 JList

发布于 2024-10-10 15:32:12 字数 213 浏览 3 评论 0原文

我正在用 Java 制作 twitter 客户端(桌面应用程序),我也使用 twitter4j API。我已经成功搜索推文并得到结果并在 Jlist 中显示它们。 我想要的是,我想在列表中很好地显示推文,而不仅仅是作为文本..显示用户的图像、推文、推文...等所有这些信息..此外还附加附加数据,例如星号评级..我如何将其添加到 JList 中? Jlist 可以容纳不同的对象.. 例如 Jpanels ..

i am making a twitter client (desktop application) in Java, i am using twitter4j API also. i have managed to do the search for tweets and i get back results and i show them in a Jlist.
what i want is that i want to show tweets nicely in the list, not only as a text .. show the image of the user, the tweet, tweeted by ... etc all this information .. in addition attach additional data like star rating .. how can i add that to a JList ? can the Jlist hold different objects .. Jpanels for example ..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可遇━不可求 2024-10-17 15:32:13

AS Jason 建议最好选择 jtable 而不是 JLIst。事实上,您可以使用任何基于 Java 的免费表类,这些表类具有比 JTables 更扩展的功能。 JIDE 就是这样一个库,但它是商业性的。你可以搜索并找到很多..

AS Jason suggested its better to go for jtable instead of JLIst. In fact you can use any free Java based table classes that have extended functionality over JTables. JIDE is one such library but its commercial. you can search and find a lot..

酸甜透明夹心 2024-10-17 15:32:12

相反,我建议您将一组 JPanel 放入 JScrollPane 中。

Instead I suggest you put a set of JPanels inside a JScrollPane.

满栀 2024-10-17 15:32:12

JList 的呈现器必须是 JComponent,因此您可以使用任何 Swing 对象,包括 JPanel。

如果比使用 JPanel 更容易的话,您还可以在 JLabel 中使用 HTML。

要使用自定义渲染器,您可以执行类似的操作...

myList.setCellRenderer(new CustomRenderer());

然后创建一个像这样的渲染器

public class CustomRenderer extends DefaultListCellRenderer {

  public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) {
    JPanel panel = new JPanel();
    // set up the panel for your exact display requirements.
    return(panel);
  }
}

A JList's renderer must be a JComponent, so you can use any Swing object, including JPanels.

You can also use HTML in a JLabel if it is easier to do so than using a JPanel.

To use a custom renderer, you do something like this..

myList.setCellRenderer(new CustomRenderer());

and then create a renderer like this

public class CustomRenderer extends DefaultListCellRenderer {

  public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) {
    JPanel panel = new JPanel();
    // set up the panel for your exact display requirements.
    return(panel);
  }
}
小镇女孩 2024-10-17 15:32:12

建议使用 JTable,它有多个列,而不是 JList。

还建议使用 GlazedList,这使得在 JTable 中显示带有字段的列表变得非常容易,以便它们更新当基础列表更改时自动。

下面是我最近编写的一些代码示例,其中显示了类似的内容:

private void bindEmailTargetTable(NotificationModel newModel) {
    JTable table = getUI(UIKey.EMAIL_TARGET_TABLE);
    EventList<EmailTarget> displayList = newModel.getEmailTargets();
    TableFormat<EmailTarget> tf = new TableFormat<EmailTarget>()
    {
        @Override public int getColumnCount() {
            return 4;
        }

        private final String[] columns = { "address", "description", "msg left", "msg limit" };
        @Override public String getColumnName(int col) {
            return this.columns[col];
        }

        @Override public Object getColumnValue(EmailTarget item, int col) {
            switch (col)
            {
                case 0:
                    return item.getAddress();
                case 1:
                    return item.getDescription();
                case 2:
                    return item.getRemainingMessages();
                case 3:
                    return item.getMessageLimit();
                default:
                    return "";
            }
        }

    };
    EventTableModel<EmailTarget> etm = new EventTableModel<EmailTarget>(displayList, tf);
    table.setModel(etm);
}   

这是 33 行代码,用于获取 JTable 并使其自动更新以显示每个 EmailTarget 的 4 个字段在 EventList 中。

对于非文本字段内容,您只需要一个自定义的 TableCellRenderer。

Suggest using a JTable, which has several columns, instead of a JList.

Also suggest using GlazedLists, which makes it really easy to display lists with fields in a JTable, so that they update automatically when the underlying list changes.

Here's an example of some code I wrote recently which displays something similar:

private void bindEmailTargetTable(NotificationModel newModel) {
    JTable table = getUI(UIKey.EMAIL_TARGET_TABLE);
    EventList<EmailTarget> displayList = newModel.getEmailTargets();
    TableFormat<EmailTarget> tf = new TableFormat<EmailTarget>()
    {
        @Override public int getColumnCount() {
            return 4;
        }

        private final String[] columns = { "address", "description", "msg left", "msg limit" };
        @Override public String getColumnName(int col) {
            return this.columns[col];
        }

        @Override public Object getColumnValue(EmailTarget item, int col) {
            switch (col)
            {
                case 0:
                    return item.getAddress();
                case 1:
                    return item.getDescription();
                case 2:
                    return item.getRemainingMessages();
                case 3:
                    return item.getMessageLimit();
                default:
                    return "";
            }
        }

    };
    EventTableModel<EmailTarget> etm = new EventTableModel<EmailTarget>(displayList, tf);
    table.setModel(etm);
}   

That's 33 lines of code to take a JTable and make it automatically update itself to display 4 fields of each EmailTarget in an EventList<EmailTarget>.

For non-text field contents, you just need a custom TableCellRenderer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文