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 2005

Tip: Looking for answers? Try searching our database.

Getting my first custom JList to work

Thread view: 
Wes Harrison - 04 Nov 2005 14:47 GMT
I am trying to get a JList to display each item as a JPanel consisting of
three JLabels arranged in a GridLayout of 1 row and 3 columns.  I have
created a class that encapsulates each cell (and inherits JPanel) and have
copied DefaultListCellRenderer to a new class CustomListCellRenderer  that
inherits from this cell class.  I have also set the list cell renderer for
the JList in the main program.  However, nothing is displayed in the list
when I run the program.

Do I have to override paintComponent() in my CustomListCellRenderer class as
well to get it to draw something?  I would have thought that it would know
how to render the class given that it is a JPanel.  Are there any other
steps that I may be missing?

Thanks,

Wes
Wes Harrison - 04 Nov 2005 15:46 GMT
> I am trying to get a JList to display each item as a JPanel consisting of
> three JLabels arranged in a GridLayout of 1 row and 3 columns.  I have
[quoted text clipped - 8 lines]
> how to render the class given that it is a JPanel.  Are there any other
> steps that I may be missing?

Hmm, I can get a custom list (a different one) to work if I use JLabel as
the base type for my CustomListCell but not when I use JPanel.  The list
entry is being displayed (ie. I can select it) but there is nothing in it.
Which begs me to ask, can I use a JPanel to display items in a JList?  Maybe
there is a restriction I am unaware of.  In my CustomListCellRenderer I
explicitly set the text of all 3 JLabels but they are displaying as blank.

Thanks,

Wes
Michael Dunn - 04 Nov 2005 19:37 GMT
>> I am trying to get a JList to display each item as a JPanel consisting of
>> three JLabels arranged in a GridLayout of 1 row and 3 columns.  I have
[quoted text clipped - 21 lines]
> there is a restriction I am unaware of.  In my CustomListCellRenderer I
> explicitly set the text of all 3 JLabels but they are displaying as blank.

works OK like this

import javax.swing.*;
import java.awt.*;

class Testing extends JFrame
{
 DefaultListModel dlm = new DefaultListModel();
 JList list = new JList(dlm);
 public Testing()
 {
   setLocation(400,300);
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   list.setCellRenderer(new MyRenderer());
   JScrollPane sp = new JScrollPane(list);
   sp.setPreferredSize(new Dimension(100,200));
   getContentPane().add(sp);
   pack();
   for(int x = 1; x < 100; x += 3) dlm.addElement(new
String[]{""+x,""+(x+1),""+(x+2)});
 }
 public static void main(String[] args){new Testing().setVisible(true);}
}

class MyRenderer extends JPanel implements ListCellRenderer
{
 JLabel[] lbl = new JLabel[3];
 public MyRenderer()
 {
   setLayout(new GridLayout(0,3));
   for(int x = 0; x < lbl.length; x++)
   {
     lbl[x] = new JLabel();
     lbl[x].setOpaque(true);
     add(lbl[x]);
   }
 }
 public Component getListCellRendererComponent(JList list,Object value,
                     int index,boolean isSelected,boolean cellHasFocus)
 {
   for(int x = 0; x < lbl.length; x++)
   {
     lbl[x].setText((String)((String[])value)[x]);
     if(cellHasFocus) lbl[x].setBackground(Color.CYAN);
     else lbl[x].setBackground(Color.WHITE);
   }
   return this;
 }
}


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.