tags:

views:

21

answers:

1

I have a java applet in which I have to display a large amount of items (dictionary entries). The user needs to be able to select individual items in the list, hence it is implemented as a JList. Generating the list was very quick until I decided to make the display more asthetically pleasing by formatting the individual items using HTML. Now the list looks pretty but it takes between 10 and 15 seconds to generate it each time the user accesses the dictionary (without formatting it occurs almost instantly). I suppose I could take the performance hit up front by generating the list when the user first enters the application and just hiding and unhiding the list as needed. But, I'm wondering if there is a better way. Perhaps a more efficient way to generate the list.

Here is the section of code where the slow down occurrs (Between the display of C and D):

DefaultListModel dictCodeModel =  new DefaultListModel();
System.out.println("C");
while (e.hasMoreElements()) {
String currentEntry = "";
DEntry dentry = (DEntry) e.nextElement();
if (!filter)                 
  dictCodeModel.addElement(dentry.theToken); // tokens have embedded html tags

}
System.out.println("D");

As you can see it is pretty simple. When "theToken" is formatted as HTML, I get a real performance hit. Any ideas of what I can do to get around this?

Thanks,

+1  A: 

What kind of HTML formatting are you using? If it's just some text styling (font, color), you can use a JLabel, set its properties accordingly and set it as ListCellRenderer for the JList.

Michael Borgwardt
I see where you are going with this, but I need something that will allow me to display a multiple line entry. Here is a typical formatted entry:<html><DL> <DT>E995.9 Injury due to war operations by other and unspecified forms of conventional warfare<DD> Unspecified form of conventional warfare</DL></html>Inserting newlines \n or return characters \r doesn't seem to work with JList even with a ListCellRenderer. Suggestions?
Elliott
@Elliott: you could use a custom component (or even just a custom LabelUI: http://codeguru.earthweb.com/java/articles/198.shtml) to render multiline text while avoiding HTML parsing (I'd first profile to confirm whether that is actually the bottleneck). Alternatively, you could try using a single-column JTable instead of a JList; IIRC JTable is designed for large amounts of data much more than JList and renders only the elements that are actually visible.
Michael Borgwardt
I wound up using a JTextArea component to display the text. It did the trick. The ListCellRenderer suggestion though pointed me in the right direction. I used the following sample code to help me get through this:http://forums.sun.com/thread.jspa?threadID=552711
Elliott