Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #2781 > unrolled thread

ListCellRenderer Only Pai

Started by"Eric Czarny" <eric.czarny@THRWHITE.remove-dii-this>
First post2011-04-27 15:41 +0000
Last post2011-04-27 15:41 +0000
Articles 5 — 2 participants

Back to article view | Back to comp.lang.java.gui


Contents

  ListCellRenderer Only Pai "Eric Czarny" <eric.czarny@THRWHITE.remove-dii-this> - 2011-04-27 15:41 +0000
    Re: ListCellRenderer Only "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:41 +0000
      Re: ListCellRenderer Only "Eric Czarny" <eric.czarny@THRWHITE.remove-dii-this> - 2011-04-27 15:41 +0000
        Re: ListCellRenderer Only "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:41 +0000
          Re: ListCellRenderer Only "Eric Czarny" <eric.czarny@THRWHITE.remove-dii-this> - 2011-04-27 15:41 +0000

#2781 — ListCellRenderer Only Pai

From"Eric Czarny" <eric.czarny@THRWHITE.remove-dii-this>
Date2011-04-27 15:41 +0000
SubjectListCellRenderer Only Pai
Message-ID<e3115772-f363-403c-be6a-462b55161ca9@y5g2000hsf.googlegroups.com>
  To: comp.lang.java.gui
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?

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#2782 — Re: ListCellRenderer Only

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:41 +0000
SubjectRe: ListCellRenderer Only
Message-ID<7b552980dbe5a@uwe>
In reply to#2781
  To: comp.lang.java.gui
Eric Czarny wrote:
..
>...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>

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200711/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2783 — Re: ListCellRenderer Only

From"Eric Czarny" <eric.czarny@THRWHITE.remove-dii-this>
Date2011-04-27 15:41 +0000
SubjectRe: ListCellRenderer Only
Message-ID<885e76e0-a952-415a-bbaa-be773da393aa@b15g2000hsa.googlegroups.com>
In reply to#2782
  To: comp.lang.java.gui
On Nov 17, 12:12 am, "Andrew Thompson" <u32984@uwe> wrote:
> Eric Czarny wrote:
>
> ..
>
> >...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>
>
> --
> Andrew Thompsonhttp://www.athompson.info/andrew/
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-gui/200711/1

I forgot all about MediaTracker, thanks for the link!

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2785 — Re: ListCellRenderer Only

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:41 +0000
SubjectRe: ListCellRenderer Only
Message-ID<7b5580f7ef227@uwe>
In reply to#2783
  To: comp.lang.java.gui
Eric Czarny wrote:
..
>I forgot all about MediaTracker, thanks for the link!

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

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200711/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2786 — Re: ListCellRenderer Only

From"Eric Czarny" <eric.czarny@THRWHITE.remove-dii-this>
Date2011-04-27 15:41 +0000
SubjectRe: ListCellRenderer Only
Message-ID<15f1be8a-01d1-45d1-8765-6f20edf4edfc@n20g2000hsh.googlegroups.com>
In reply to#2785
  To: comp.lang.java.gui
On Nov 17, 12:51 am, "Andrew Thompson" <u32984@uwe> wrote:
> Eric Czarny wrote:
>
> ..
>
> >I forgot all about MediaTracker, thanks for the link!
>
> Knute's code examples are excellent, for quickly
> explaining these sorts of questions.  ;-)
>
> --
> Andrew Thompsonhttp://www.athompson.info/andrew/
>
> 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?

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web