Re: efficiency of JList setElementAt()



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/
.



Relevant Pages

  • Re: Creating a "movie" in Swing.
    ... get about 1 frame a second and I see about every 15th frame. ... public void paint{ ... can do to prevent the write coalescing with repaint(). ...
    (comp.lang.java.programmer)
  • followup Q Re: Simple code sample wanted: enum as method parameter
    ... enum PageReturnType ... static void ArchiveRemotePageAsPDF ... public void ArchiveRemotePageAsPDF ... > static void DoSomething(Foo foo) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH][RFC] 2.6.6 tty_io.c hangup locking
    ... static void ppp_sync_input(struct syncppp *ap, const unsigned char *buf, ... * a whole frame. ...
    (Linux-Kernel)
  • [PATCH][RFC] 2.6.6 ppp_synctty.c
    ... static void ppp_sync_flush_output(struct syncppp *ap); ... static void ppp_sync_input(struct syncppp *ap, const unsigned char *buf, ... * a whole frame. ...
    (Linux-Kernel)
  • Re: subclass JPasswordField
    ... public static void main ... class TimedPasswordListener implements DocumentListener, ActionListener { ... public void showText{ ... // How do I set the echo char of the JPasswordField ...
    (comp.lang.java.programmer)