Re: efficiency of JList setElementAt()



Thanks Tom. I agree with your results when I run your program so that made
me immediately consider something that I hadn't considered important before.
My list objects are HTML strings solely for the purpose of highlighting a
few words in a different color. This appears to make the list update about
an order of magnitude more costly! If you modify your program to produce
strings about 3 times as long, make the strings HTML with a font color tag,
and increase the list size to about 130, I think you'll get the kind of
results I cited in my first post. (Note: by virtue of the fact that your
program did not compile with my 1.4.2 JDK, I suspect you are using an
earlier Java version and I know that HTML support has been introduced over
time so I'm not sure if you'll be able to use HTML strings).

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?

Thanks again for your persistence.

RC

"Thomas Hawtin" <usenet@xxxxxxxxxxxxxxxxx> wrote in message
news:43a74833$0$1467$ed2619ec@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Raymond Cruz wrote:
> >
> > The problem isn't very sensitive to how much is on the screen. For the
> > examples given, approximately 75% of the list was outside of the
scroller's
> > viewport. In fact, if the frame that displays the JList is minimized to
a
> > null display, the timing improves by only about 15%.
>
> You probably need to use a profiler on it (or do a ctrl-\ or ctrl-break
> on it randomly from the console). Possibly it is something to do with
> validating the layout, the model/list elements might be slow or perhaps
> you are extremely short on video RAM.
>
> I wrote a little benchmark. On 266 MHz PII it settled down to 3-3.5%
> CPU. On my 2.26ish GHz Celeron, it came in comfortably under 1%.
>
> import java.awt.event.ActionEvent;
> import java.awt.event.ActionListener;
> import javax.swing.*;
>
> class ListSpeed {
> public static void main(String[] args) {
> java.awt.EventQueue.invokeLater(new Runnable() {
> public void run() {
> swing();
> }
> });
> }
> private static final java.util.Random random =
> new java.util.Random();
> private static void swing() {
> assert java.awt.EventQueue.isDispatchThread();
> JFrame frame = new JFrame("ListSpeed");
> final DefaultListModel model = new DefaultListModel();
> for (int ct=0; ct<50; ++ct) {
> model.addElement("some data "+new java.util.Date());
> }
> final JList list = new JList(model);
> frame.add(list);//new JScrollPane(list));//
> frame.pack();
> frame.setVisible(true);
> new javax.swing.Timer(1000 /* millis */, new ActionListener() {
> private boolean set;
> public void actionPerformed(ActionEvent event) {
> model.setElementAt(
> "new data "+new java.util.Date(),
> random.nextInt(model.size())
> );
> }
> }).start();
> }
> }
>
> Tom Hawtin
> --
> Unemployed English Java programmer
> http://jroller.com/page/tackline/


.