Re: efficiency of JList setElementAt()



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>


.



Relevant Pages

  • JList goes blank after update ListModel
    ... I have a JList created with a DefaultListModel and a custom cell renderer. ... JLabel knows all about, but which is obscured by the coding style of Swing ... There may be a bug in JList that it calls my paint method too ...
    (comp.lang.java.gui)
  • JTable: auto resize row height
    ... In a classic JTable, with default cell renderer, if cell's value is ... too big to fit, the default renderer shows some text and "...". ... and WrapWordStyle active but I need to resize JTextArea when it adds a ...
    (comp.lang.java.gui)
  • Re: JTable - Change Cell / Row Color.
    ... It's the cells you have to manipulate using a custom cell renderer. ... DefaultTableCellRenderer and override ... component each time getTableCellRendererComponent. ...
    (comp.lang.java.gui)
  • Re: Paint a stickman in a JTable cell
    ... Is it easy to draw polygons, arcs and circles in a cell in a table, ... > possibly by using an overriding paintmethod? ... but you can return a JPanel instance from a cell renderer. ...
    (comp.lang.java.gui)
  • VB, implementing the high score within this code
    ... Private possibleAs String ... Private Function SolvePuzzleAs Boolean ... ' Calculates the possible values for all the cell ...
    (microsoft.public.dotnet.languages.vb)