Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2007

Tip: Looking for answers? Try searching our database.

ListCellRenderer Only Paints the First Cell

Thread view: 
Eric Czarny - 17 Nov 2007 04:58 GMT
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?
Andrew Thompson - 17 Nov 2007 05:11 GMT
...
>...Has anyone else witnessed a similar problem?

Images load asynchronously.  Here are some examples
of image loading.  <http://www.knutejohnson.com/>
Look particularly at the 'How to load images' examples.

Other things I would recommend changing about those
code snippets are:
- Instantiate a JLabel, once in the constructor of the renderer.
- Use the JLabel instead of MyCell to render the image.
- Load the image only once.

Note also that posting an SSCCE* that refers to images
on the internet, allows others to see the problem, quickly
& easily.

* <http://www.physci.org/codes/sscce.html>

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Eric Czarny - 17 Nov 2007 05:40 GMT
> ..
>
[quoted text clipped - 20 lines]
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-gui/200711/1

I forgot all about MediaTracker, thanks for the link!
Andrew Thompson - 17 Nov 2007 05:51 GMT
...
>I forgot all about MediaTracker, thanks for the link!

Knute's code examples are excellent, for quickly
explaining these sorts of questions.  ;-)

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Eric Czarny - 17 Nov 2007 20:20 GMT
> ..
>
[quoted text clipped - 7 lines]
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-gui/200711/1

I'm still having trouble getting the JList to render more than one
cell. The cells are actually there (I can select them, etc...), but it
doesn't look like the paintComponent method has any affect. Any ideas
as to why this happens? Is there some behavior of ListCellRenderer I
am overlooking?
Eric Czarny - 17 Nov 2007 22:59 GMT
> > ..
>
[quoted text clipped - 13 lines]
> as to why this happens? Is there some behavior of ListCellRenderer I
> am overlooking?

I found a solution to my problem and decided to post the solution for
anybody else who runs into this problem. It turns out I was drawing in
relation to the wrong point of origin. By removing the calls to getX
and getY in paintComponent I was able to draw in the correct places.

I feel a little dumb.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.