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


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

Re: Generics ?

From "John B. Matthews" <nospam@nospam.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Generics ?
Date 2011-12-17 11:18 -0500
Organization The Wasteland
Message-ID <nospam-B23F53.11184417122011@news.aioe.org> (permalink)
References <jcem2m$gbm$1@dont-email.me>

Show all headers | View raw


In article <jcem2m$gbm$1@dont-email.me>,
 Knute Johnson <nospam@knutejohnson.com> wrote:

> Using Java 7, given the class file:
> 
> import javax.swing.*;
> 
> public class KList extends JList {
>      ListModel model = new DefaultListModel();
> 
>      public KList() {
>          setModel(model);
>      }
> }

I defined a generic subclass of JList:

  class KList<E> extends JList<E> {

      public KList(ListModel<E> model) {
          this.setModel(model);
      }
  }

Then I created a ListModel<String>:

  DefaultListModel<String> dlm = new DefaultListModel<>();
  dlm.add(...);

Then I used it to create a new KList:

  KList<String> list = new KList<>(dlm);

Here's my sscce:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;

/** @see http://stackoverflow.com/a/5372169/230513 */
public class JakesList {

    private static final DateFormat df =
        new SimpleDateFormat("dd-MMM-yyyy");

    private static class KList<E> extends JList<E> {

        public KList(ListModel<E> model) {
            this.setModel(model);
        }
    }

    private static void createAndShowGUI() {
        DefaultListModel<String> dlm = new DefaultListModel<>();
        for (int i = 0; i < 10; i++) {
            GregorianCalendar knownDate = new GregorianCalendar();
            knownDate.add(Calendar.DAY_OF_MONTH, 3 * i);
            dlm.add(i, df.format(knownDate.getTime()));
        }
        KList<String> list = new KList<>(dlm);
        JOptionPane.showMessageDialog(null, list);
    }

    public static void main(String args[]) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

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


Thread

Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-15 21:48 -0800
  Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-15 22:03 -0800
    Re: Generics ? Tassilo Horn <tassilo@member.fsf.org> - 2011-12-16 08:30 +0100
      Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-16 00:08 -0800
  Re: Generics ? Roedy Green <see_website@mindprod.com.invalid> - 2011-12-16 02:10 -0800
    Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-16 22:01 -0800
      Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-16 23:30 -0800
        Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-17 10:20 -0800
          Re: Generics ? markspace <-@.> - 2011-12-17 10:54 -0800
            Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-17 11:25 -0800
              Re: Generics ? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-17 21:20 -0800
                Re: Generics ? "Qu0ll" <Qu0llSixFour@gmail.com> - 2011-12-18 21:08 +1100
                Re: Generics ? Lew <lewbloch@gmail.com> - 2011-12-18 08:17 -0800
                Re: Generics ? markspace <-@.> - 2011-12-18 08:43 -0800
          Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-17 11:33 -0800
            Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-17 11:40 -0800
              Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-17 11:47 -0800
              Re: Generics ? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-17 15:13 -0500
      Re: Generics ? Roedy Green <see_website@mindprod.com.invalid> - 2011-12-17 02:46 -0800
  Re: Generics ? "John B. Matthews" <nospam@nospam.invalid> - 2011-12-17 11:18 -0500
    Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-17 11:16 -0800

csiph-web