ListCellRenderer Only Paints the First Cell
- From: Eric Czarny <eczarny@xxxxxxxxx>
- Date: Fri, 16 Nov 2007 20:58:05 -0800 (PST)
I am trying to create a ListCellRenderer that renders custom cells
extending from JComponent. The ListCellRenderer I am implementing just
creates a new instance of a JComponent subclass I have written, called
MyCell. My getListCellRendererComponent method is provided below:
public Component getListCellRendererComponent(JList list, Object
value, int index,
boolean isSelected, boolean hasFocus) {
MyCell renderedCell = new MyCell(400, 50);
renderedCell.setIndex(index);
if (isSelected) {
renderedCell.setBackground(list.getSelectionBackground());
renderedCell.setForeground(list.getSelectionForeground());
} else {
renderedCell.setBackground(Color.WHITE);
renderedCell.setForeground(Color.BLACK);
}
return renderedCell;
}
MyCell is just a basic JComponent subclass that overrides the
paintComponent method:
protected void paintComponent(Graphics graphics) {
graphics.setColor(new Color(225, 225, 225));
graphics.drawImage(Resource.getImage("icons", "snippet-edit"),
this.getX() + 25, this.getY() + 5, null);
graphics.drawLine(this.getX(), this.getY() + 49, this.getX() + 425,
this.getY() + 49);
}
The MyCell constructor just sets the preferred size of the cell
itself.
My problem is that when I create a JList using this ListCellRenderer
only the first cell in the list is rendered, and the Image I draw in
paintComponent does not appear initially. I have to either click on
the JList or move the scrollbar in order to make it appear. I have
spent a good deal of time looking for some answers, and I feel like
there is some simple remedy for this problem, but I just can't find
any hints. Has anyone else witnessed a similar problem?
.
- Prev by Date: Re: PDF Processing
- Next by Date: Re: jpanel export as pbm image
- Previous by thread: jpanel export as pbm file
- Next by thread: Re: jpanel export as pbm image
- Index(es):