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


Groups > comp.lang.java.programmer > #20820

Re: Generic JList and ListCellRenderer?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From Knute Johnson <nospam@knutejohnson.com>
Newsgroups comp.lang.java.programmer
Subject Re: Generic JList and ListCellRenderer?
Date Sun, 30 Dec 2012 08:27:49 -0800
Organization A noiseless patient Spider
Lines 66
Message-ID <kbpq25$f1f$1@dont-email.me> (permalink)
References <kbkqb6$uu3$1@dont-email.me> <nospam-49C732.20563528122012@news.aioe.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Sun, 30 Dec 2012 16:27:49 +0000 (UTC)
Injection-Info mx04.eternal-september.org; posting-host="9b3fcb0d22708969e4dc99e7aa0ef1f9"; logging-data="15407"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18s5xA04h73rDPpxM5IOW2g"
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0
In-Reply-To <nospam-49C732.20563528122012@news.aioe.org>
Cancel-Lock sha1:en/qMrs/QiNqafji38dwCvF286E=
Xref csiph.com comp.lang.java.programmer:20820

Show key headers only | View raw


On 12/28/2012 5:56 PM, John B. Matthews wrote:
> In article <kbkqb6$uu3$1@dont-email.me>,
>   Knute Johnson <nospam@knutejohnson.com> wrote:
>
>> I've been trying to clean up some really old code and I've hit some
>> snags.  I've got several modified JLists and the ListCellRenderers
>> for them and thought it would make sense to have generic classes that
>> could be extended for different data types that need to be displayed.
>>   The example below displays InetAddresses in the JList.  I've got
>> another implementation of JList that does a lot more than what this
>> one does but I wanted to keep this example simple and focus on the
>> ListCellRenderer.
>
> Good use of a class literal as runtime type token:
>
> <http://docs.oracle.com/javase/tutorial/extra/generics/literals.html>
>
>> MyListCellRenderer extends the getListCellRenderer method of
>> DefaultListCellRenderer and adds a new method, textToDisplay().  I
>> added a field to the constructor that specifies the class of the data
>> element to be displayed.  That class information is the test to make
>> the call to the textToDisplay() method.  The
>> InetAddressListCellRenderer class extends MyListCellRenderer to get
>> this to display nice neat Strings for the InetAddresses.
>>
>> I think this whole thing is a little kludgie.  I've been playing with
>> it for so long now I'm not getting anywhere anymore.  I was hoping
>> somebody would say "gee you ought to go this way" or "no that is
>> really brilliant code my man!" and I'll leave it as is :-).
>>
>> Anyway, please take a look and if you have any great ideas, I would
>> appreciate the feedback.
>
> Some suggestions:
>
> Use the @Override annotation.
>
> Use the for-each (Iterable) loop in setListData():
>
> @Override
> public void setListData(Vector<? extends E> v) {
>      model.clear();
>      for (E e : v) {
>          model.addElement(e);
>      }
> }
>
> Consider supporting List:
>
> public void setListData(List<? extends E> v) {
>      model.clear();
>      for (E e : v) {
>          model.addElement(e);
>      }
> }
>
> Catch the more specific exception:
>
> catch (UnknownHostException e)
>

Thanks John.

-- 

Knute Johnson

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Generic JList and ListCellRenderer? Knute Johnson <nospam@knutejohnson.com> - 2012-12-28 11:01 -0800
  Re: Generic JList and ListCellRenderer? "John B. Matthews" <nospam@nospam.invalid> - 2012-12-28 20:56 -0500
    Re: Generic JList and ListCellRenderer? Knute Johnson <nospam@knutejohnson.com> - 2012-12-30 08:27 -0800
  Re: Generic JList and ListCellRenderer? "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2012-12-29 15:11 +0000
    Re: Generic JList and ListCellRenderer? Knute Johnson <nospam@knutejohnson.com> - 2012-12-30 08:29 -0800

csiph-web