开发者

JLists with a set of objects

开发者 https://www.devze.com 2023-02-02 08:33 出处:网络
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.

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 ? 开发者_StackOverflow中文版can the Jlist hold different objects .. Jpanels for example ..


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


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


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.


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..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号