This site requires JavaScript, please enable it in your browser!
Greenfoot back
Yehuda
Yehuda wrote ...

2017/4/25

DefaultListModel

Yehuda Yehuda

2017/4/25

#
I have a JList and a DefaultListModel, created like this:
1
2
3
private DefaultListModel usersListModel = new DefaultListModel();
private JList usersJList = new JList(usersListModel);
private JScrollPane usersScrollPane = new JScrollPane(usersJList);
I didn't use the <> for the classes. I want to know how I can (if I can) have an element in the list which has a String and GreenfootImage in it. Right now I'm adding things into the list like this:
1
usersListModel.addElement("Test Text");
Just doing this:
1
usersListModel.addElement(new GreenfootImage(10, 10));
shows "greenfoot.GreenfootImage@<numbers and lowercase letters>" in the list. I want to get the list to know that it's supposed to show an image, not only text (and display the image not just "get the list to know").
danpost danpost

2017/4/25

#
Yehuda wrote...
I have a JList and a DefaultListModel, .... I want to know how I can (if I can) have an element in the list which has a String and GreenfootImage in it. .... I want to get the list to know that it's supposed to show an image, not only text (and display the image not just "get the list to know").
You can have both String text and images from a GreenfootImage object diplay in the JList object. Of course, using 'new GreenfootImage(10, 10)' will only take up some space in the list once you get the image to show, but you will not see it (as the image is totally transparent). However, if you did have one:
1
GreenfootImage image;
whose image was not totally transparent (and not the same color as the background of the JList), the JList will display it, and you will see it, with the following code:
1
usersListModel.addElement(new ImageIcon(image.getAwtImage()));
Yehuda Yehuda

2017/4/26

#
Thanks. But I want to be able to have a String and a GreenfootImage in the same element (and I don't want to have to do drawString( for my String).
danpost danpost

2017/4/26

#
Yehuda wrote...
Thanks. But I want to be able to have a String and a GreenfootImage in the same element (and I don't want to have to do drawString( for my String).
Sorry. I understand now. You will probably need to use either 'drawString' or 'drawImage' to create an image with both the icon and the text and then use the above to add the element.
Yehuda Yehuda

2017/4/27

#
Why "either 'drawString' or 'drawImage'"? I need to create one big image and then do both on it if I can only add either an image or a String but not both at the same time (in an element - which is what I want to do).
danpost danpost

2017/4/27

#
Yehuda wrote...
Why "either 'drawString' or 'drawImage'"? I need to create one big image and then do both on it if I can only add either an image or a String but not both at the same time (in an element - which is what I want to do).
Using 'drawString' is not compulsory. You can create a GreenfootImage of the text and use 'drawImage' to also (as well as using it to draw the icon) draw the text on the larger image. (I guess not compulsory to use 'drawImage' either, as you could also grab each pixel from each of the smaller images and use 'setColorAt' on the the larger one to avoid using either of the aforementioned methods; but, that would be a bit much).
Yehuda Yehuda

2017/4/27

#
danpost wrote...
Using 'drawString' is not compulsory. You can create a GreenfootImage of the text and use 'drawImage' to also (as well as using it to draw the icon) draw the text on the larger image. (I guess not compulsory to use 'drawImage' either, as you could also grab each pixel from each of the smaller images and use 'setColorAt' on the the larger one to avoid using either of the aforementioned methods; but, that would be a bit much).
I was thinking of making one big (wide) image then drawing the text and image onto that. What does this method do?
danpost danpost

2017/4/27

#
Yehuda wrote...
What does this method do?
It returns the object that is responsible for painting the items (or cells) in a JList object.
Yehuda Yehuda

2017/4/27

#
danpost wrote...
It returns the object that is responsible for painting the items (or cells) in a JList object.
It's a void method so it can't return anything. I'm sorry I didn't specify which method I'm talking about. http://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html#setCellRenderer-javax.swing.ListCellRenderer-
Java API wrote...
Painting of cells in a JList is handled by a delegate called a cell renderer, installed on the list as the cellRenderer property. The renderer provides a java.awt.Component that is used like a "rubber stamp" to paint the cells. Each time a cell needs to be painted, the list's ListUI asks the cell renderer for the component, moves it into place, and has it paint the contents of the cell by way of its paint method. A default cell renderer, which uses a JLabel component to render, is installed by the lists's ListUI. You can substitute your own renderer using code like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Display an icon and a string for each object in the list.
 
class MyCellRenderer extends JLabel implements ListCellRenderer<Object> {
    final static ImageIcon longIcon = new ImageIcon("long.gif");
    final static ImageIcon shortIcon = new ImageIcon("short.gif");
 
    // This is the only method defined by ListCellRenderer.
    // We just reconfigure the JLabel each time we're called.
 
    public Component getListCellRendererComponent(
      JList<?> list,           // the list
      Object value,            // value to display
      int index,               // cell index
      boolean isSelected,      // is the cell selected
      boolean cellHasFocus)    // does the cell have focus
    {
        String s = value.toString();
        setText(s);
        setIcon((s.length() > 10) ? longIcon : shortIcon);
        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        setEnabled(list.isEnabled());
        setFont(list.getFont());
        setOpaque(true);
        return this;
    }
}
 
myList.setCellRenderer(new MyCellRenderer());
Another job for the cell renderer...
danpost danpost

2017/4/27

#
Yehuda wrote...
Another job for the cell renderer...
Well, same job; just different implementation and/or added behavior. As far as what the method does, it gives the component a custom cell renderer.
Yehuda Yehuda

2017/4/28

#
I managed to get my image "in the picture" (Image and text together) like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
 * Return a component that has been configured to display the specified
 * value. That component's <code>paint</code> method is then called to
 * "render" the cell. If it is necessary to compute the dimensions of a list
 * because the list cells do not have a fixed size, this method is called to
 * generate a component on which <code>getPreferredSize</code> can be
 * invoked.
 *
 * @param list The JList we're painting.
 * @param value The value returned by list.getModel().getElementAt(index).
 * @param index The cells index.
 * @param isSelected True if the specified cell was selected.
 * @param cellHasFocus True if the specified cell has the focus.
 * @return A component whose paint() method will render the specified value.
 *
 * @see JList
 * @see ListSelectionModel
 * @see ListModel
 */
private Component usersJListGetListCellRendererComponent(JList<?> list,
        Object value,
        int index,
        boolean isSelected,
        boolean cellHasFocus) {
    JLabel theLabel = new JLabel();
    theLabel.setText(value.toString());
    theLabel.setIcon(new ImageIcon(/*the GreenfootImage*/.getAwtImage()));
    if (isSelected) {
        theLabel.setBackground(list.getSelectionBackground());
        theLabel.setForeground(list.getSelectionForeground());
    } else {
        theLabel.setBackground(list.getBackground());
        theLabel.setForeground(list.getForeground());
    }
    theLabel.setEnabled(list.isEnabled());
    theLabel.setFont(list.getFont());
    theLabel.setOpaque(true);
    return theLabel;
}
danpost danpost

2017/4/28

#
In hindsight, I should have taken at least a quick glance at the JLabel class documentation. I would have seen that was possible.
Yehuda Yehuda

2017/4/28

#
danpost wrote...
In hindsight, I should have taken at least a quick glance at the JLabel class documentation. I would have seen that was possible.
I wouldn't have. I only knew about doing this because of what I posted above (JList API). With that coding the image is always on the left side of the element. Is there a way for me to specify where in the element it's placed?
Yehuda Yehuda

2017/4/30

#
This can (also) use some assistance.
Yehuda wrote...
With that coding the image is always on the left side of the element. Is there a way for me to specify where in the element it's placed?
You need to login to post a reply.