hi, im trying to write a custom ListCellRenderer in order to allow me to create a JList of components extended from a JPanel (the "Item" class). My current attempt is displayed below, however i admit i am not so farmiliar with Java this advanced (so it may seem a little simple). The program currently is set up to add Item objects to a JList nested within a JScrollPane, with some success. The JPanels of the Item class are visible within the JScrollPane when run, however the text intended to appear inside a JLabel in the centre of the panels does not appear. In addition to this it is noted that a significant delay is observed upon running of the program.
I was hoping the "getListCellRendererComponent()" method shown below would create a new instance of Item(), using the values of the one passed to the ListCellRenderer implementation, with a view to returnin a duplicate to add render within the list. However this does not quite seem to work. In addition a printline added within the method indicates it is called more times than is neccissary (such as when the mouse clicks on a panel, or when the tab the components are shown in is selected). However in the case of the latter i expect this is just java painting the components to the screen (as i believe that is the reason for creating and returning the object within the method in the first place). Thus i believe my understanding of custom rendering falls short and was wondering if anyone could suggest how my code could be ammended (since it does not work ;)) and could explain what is supposed to be happening here. I appraciate my knowledge of waht i am attempting to do is relativley superficial, since i have no knowledge of rendering and have simply based my understanding from the explanations presented on the tutorials found on this site (where i am mimicing what the code seems to do as much as understanding it). However i am interested in learning as much as possible about this aspect of java and as an additonal qeustion, if anyone is farmiliar with any other tutorials or articles on rendering in general within java i would be appreciative :).
please forgive the poor style (i am lazy)
thanks
listModel = new DefaultListModel();
list = new JList(listModel);
list.setCellRenderer(new MyListRenderer());
...
listModel.addElement(item);
...
class MyListRenderer extends Item implements ListCellRenderer {
public MyListRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
this.label.setText(((Item)value).name);
System.out.println("a println");
return this;
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Item extends JPanel
{
JLabel label;
String name;
public Item()
{
Dimension d8 = new Dimension();
d8.height = 30;
d8.width = 200;
this.setPreferredSize(d8);
this.setMaximumSize(d8);
this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
label = new JLabel("");
}
public Item(String name)
{
Dimension d8 = new Dimension();
d8.height = 30;
d8.width = 200;
this.setPreferredSize(d8);
this.setMaximumSize(d8);
this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
label = new JLabel(name);
this.name = name;
this.add(label);
}
public void itemName(String name)
{
label.setText(name);
}
}
Edited by: 806418 on 30-Oct-2010 07:04
Edited by: 806418 on 30-Oct-2010 07:05