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


Groups > comp.lang.java.gui > #779

Re: JList items

From "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this>
Subject Re: JList items
Message-ID <ep5srj$uv0$1@registered.motzarella.org> (permalink)
Newsgroups comp.lang.java.gui
References <1169573019.407108.250060@a75g2000cwd.googlegroups.com>
Date 2011-04-27 15:29 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
Ooder schrieb:
> Hi,
> 
> I have a JList that contains a list of steps to do something.  When the
> user completes a step they then select the next step, and the previous
> step is considered complete.  Is there a way to format an individual
> JList item, so I could visually display that a step is done (italic
> text, unenabled, gray background, etc)?  Do I have to create a custom
> ListCellRenderer?  If so, how, since I need to be able to customize
> individual items in the list?

One possibility would be to not use JList but JTable having two columns 
for both, the step and the status of the task.

If you want to use JList, then you would have to use a custom cell 
renderer. The "how to" is something that you must decide on your own. In 
general, a cell renderer performs only the... rendering :) Therefore the 
status of a task is not stored by the renderer but is represented by the 
value it should render.

Depending on what you currently have you'll need a class that represents 
a task and the task's status:

class TaskEntry {
     private String title;
     private boolean done;

     ...
}

The ListModel returns objects of type TaskEntry. The cell renderer 
handles them, e. g.

class TaskEntryRenderer extends DefaultListCellRenderer {
     private Color OUTSTANDING = Color.BLACK;
     private Color DONE = Color.GREEN;

     public Component getListCellRendererComponent( JList list,
             Object value, int index, boolean sel, boolean focus ) {
         TaskEntry entry = (TaskEntry)value;
         Component c = super.getListCellRendererComponent( list,
                 entry.getTitle(), index, sel, focus );
         c.setColor( entry.isDone() ? DONE : OUTSTANDING );
         return c;
     }
}

Bye
Michael

---
 * 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

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


Thread

JList items "Ooder" <ooder@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000
  Re: JList items "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000
    Re: JList items "Ooder" <ooder@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000

csiph-web