Re: efficiency of JList setElementAt()
- From: "Raymond Cruz" <nospam@xxxxxxxxxxx>
- Date: Thu, 22 Dec 2005 17:11:37 GMT
I wrote a cell renderer tailored to my application and it has resulted in a
performance improvement by at least a factor of 5. I include it below in
case it might be useful to anyone and for a critique if anyone is so
motivated since I don't consider myself to be much more than an advanced
beginner in Java.
However I still have performance questions and hopes for better performance.
The construction of my list is incremental since it builds as messages
arrive. However after some number of minutes it reaches steady state in
size and order and afterwards single cells are modified in place with new
data. My hope and expectation is that only a single cell would be
invalidated and re-rendered in this operation but it appears that either the
entire list is invalidated or all cells from the changed cell to the end of
the list are invalidated. I base this conclusion on the fact that the
performance penalty for my updates increases fairly linearly with list size.
Is it possible to achieve constant update time for such a list? What would
one need to do to achieve that goal?
RC
/*
* This class is a cell renderer that supports the capability of
* highlighting substrings in a JList of strings with a different
* color. This capability and more can be achieved by using HTML
* with a much higher performance penalty. Initially table entry
* color is black but it can be changed within the value string by
* a substring consisting of "\u00A7RRGGBB" where '\u00A7 is the
* unicode for a "section marker" and RRGGBB are the hexadecimal
* values for the red, green, and blue color components.
*/
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
public class ColorCellRenderer extends JPanel implements ListCellRenderer {
private static final long serialVersionUID = 1L;
private static final String DELIMITER = "\u00A7";
private JList list;
private String cv;
private int fontSize;
private boolean selected;
private int maxWidth = 0;
// default constructor, 12 point font
public ColorCellRenderer() {
this.fontSize = 12;
}
// this constructor allows font point size to be specified
public ColorCellRenderer(int fontSize) {
this.fontSize = fontSize;
}
// implement the abstract method of the interface
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
this.list = list;
selected = isSelected;
cv = value.toString();
return this;
}
// override of paint() to render the cell. interpret substrings
// that specify font color changes and paint the background of the
// selected cell.
public void paint(java.awt.Graphics g) {
int text_x = 0;
int text_y;
boolean colorChange = false;
g.setFont(new Font("Arial", Font.PLAIN, fontSize));
FontMetrics fm = g.getFontMetrics();
int fontHeight = fm.getHeight();
if(selected) {
g.setColor(list.getSelectionBackground());
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
g.setColor(Color.black);
text_y = fontHeight-2;
java.util.StringTokenizer st = new java.util.StringTokenizer(cv,
DELIMITER, true);
while(st.hasMoreTokens()) {
String s = st.nextToken();
if(colorChange) {
colorChange = false;
if(s.length() >= 6) {
int newColor;
String colorString = s.substring(0, 6);
try {
newColor = Integer.parseInt(colorString, 16);
} catch(Exception e) {newColor = 0;}
g.setColor(new Color(newColor));
s = s.substring(6);
}
}
if(s.equals(DELIMITER)) {
colorChange = true;
} else {
g.drawString(s, text_x, text_y);
text_x += fm.stringWidth(s);
}
}
if(text_x > maxWidth)
maxWidth = text_x;
setPreferredSize(new java.awt.Dimension(maxWidth, fontHeight+1));
g.dispose();
}
}
"Thomas Hawtin" <usenet@xxxxxxxxxxxxxxxxx> wrote in message
news:43aa04ad$0$1475$ed2619ec@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Raymond Cruz wrote:
> > <snip>
> > So I have a new question -- is there a way to produce different font
colors
> > that is much more efficient than using HTML strings in Java?
>
> It occurred to me that you can keep your HTML and make it go at a
> reasonable speed (unless you run low on memory). The Swing cell renderer
> design is based on assumptions that construction is expensive and
> updating values is cheap. Clearly, even for the humble JLabel, the
> second is not correct when the value is HTML. So let's cache.
>
> The approach I have taken is to have a renderer lazily create a
> conventional renderer for each index. <snip>
.
- Follow-Ups:
- Re: efficiency of JList setElementAt()
- From: Thomas Hawtin
- Re: efficiency of JList setElementAt()
- References:
- efficiency of JList setElementAt()
- From: Raymond Cruz
- Re: efficiency of JList setElementAt()
- From: Thomas Hawtin
- Re: efficiency of JList setElementAt()
- From: Raymond Cruz
- Re: efficiency of JList setElementAt()
- From: Thomas Hawtin
- Re: efficiency of JList setElementAt()
- From: Raymond Cruz
- Re: efficiency of JList setElementAt()
- From: Thomas Hawtin
- efficiency of JList setElementAt()
- Prev by Date: Re: JGoodies Binding
- Next by Date: Re: efficiency of JList setElementAt()
- Previous by thread: Re: efficiency of JList setElementAt()
- Next by thread: Re: efficiency of JList setElementAt()
- Index(es):
Relevant Pages
|