Re: How to add elements to a JList without preventing refresh



Thomas Hawtin a écrit :

> Anonymous user wrote:
> >
> > i have a simple problem. Let's say that in some app, i have a loop wich
> > add a lot of elements to a JList. I CANNOT change the loop, but i can
> > change how each element is added.
>
> Are you not going to tell us why?

Because i'm working on JTable and sometimes, the same error happens for
all the cells in a column for example. If the error happens in the
renderering (TableCellRenderer) then you have some kind of loop that
you can't control.

> > What i would like is that i can scroll the JList with the mouse,
> > select/unselect, etc... WHILE the elements are added:
>
> > public class Refresh extends JFrame {
>
> There is no need to extend JFrame.

For this problem and all the ones conerning the build of the frame,
blame Eclipse Visual Editor.


> If you are going to set a JPanel as the content pane, you should
> setOpaque(true) on it. It doesn't make any difference on most platforms,
> but on some it does. "Works for me" doesn't work.

good to know

> Okay, so you are are creating and adding 1,000,000 entries to the list
> in one go from within the Event Dispatch Thread.

yep

> If these entries have a high creation cost then the obvious thing to do
> is either do it in batches, or create in another thread and transfer
> across. Better still create the entries lazily, which you seem quite
> keen on (IIRC, you need set a fixed with and height on the JList, or
> they will all be created to determine individual heights). But if you
> can't do that for some reason, I guess you can't. On a 1 GHz processor
> we have around 100 cycles per entry. That should be just about doable
> for a trivial object.

i can't make it lazily because a don't know when the "loop is done". I
have no control on the loop.

> > private void add1(int i) {
> > ((DefaultListModel)getJList().getModel()).addElement(new Integer(i));
> > }
>
> Each time you add an entry, the model fires an event. That isn't going
> to be efficient. So perhaps create an entirely new model with a bulk
> add. That or remove the listeners for the duration.

good idea, i fill up a regular List, and in paintComponent(), i do:

if (regularList is not empty) {
regularList.insert(JList.this.getAll())
JList.this.setModel(new List(regularList));
// should call repaint
} else {
super.paintComponenet(g);
}

> Note, a very long list, without fixed cell width and height, validating
> layout will be extremely slow.

good to know too!

> > * The JList freezes too, and a java.lang.OutOfMemoryError is throwed;
> > */
> > private void add2(int i) {
> > SwingUtilities.invokeLater(new RunAdd(i));
> > }
>
> An anonymous inner class would have worked better here.

i don't know how add a parameter to the constructor in anonymous inner
class


> What you are doing here is queueing up a million events. That isn't
> going to be nice.

it should

> You could add the entries to a plain List. invokeLater on a task that
> adds a traunch of the outstanding entries. If not all entries are added
> in one go, then the task should invokeLater itself again. This is
> similar to the way SwingWorker operates.

i don't understand SwingWorker, i've already tried to but...

> [hack]

> > public static void main(String[] args) {
>
> You need the standard boilerplate here:
>
> EventQueue.invokeLater(new Runnable() { public void run() {
> > new Refresh().setVisible(true);
> }});
> > }
> > }

i've never managed to understand why it is needed, so i never write
it...

> Unemployed English Java programmer
hopes you get a job

> http://jroller.com/page/tackline/
seems interesting, you're in my bookmarks ;)

.



Relevant Pages

  • Re: Importing Data-Multiple Sources
    ... If all the files are in one folder, use Dir function to loop through the ... > I want to import data from multiple sources. ... > Each source has multiple entries with 6 fields in each entry. ...
    (microsoft.public.access.tablesdbdesign)
  • Re: need help with images
    ... >>this will pull out all the entries of the current month and year that we ... >>how will the tblPics table pull out all the particular images for the blog ... >>entries as there will be more than one entry on the page, ... I assume you loop through the blog entries. ...
    (alt.php)
  • RE: Creating a que
    ... and entry count in a collection. ... if eo.Count> 0 then bMoreData = True ... At the beginning of the loop you assume this is the last time through the ... Then you go through each item in the Entries ...
    (microsoft.public.excel.programming)
  • RE: Creating a que
    ... and entry count in a collection. ... if eo.Count> 0 then bMoreData = True ... At the beginning of the loop you assume this is the last time through the ... Then you go through each item in the Entries ...
    (microsoft.public.excel.programming)
  • Re: formula to count charcter in a range
    ... you use of the word "effectively" is meant to cover the fact that, for example, in "*w*w*", once one 'w' is found, the search loop must continue on in order to look for the next 'w'. ... Your formula would iterate through range as many times as there are ... argument contains wildcards isn't as simple as my SUBSTITUTE call. ... in range, A were the number of sequential entries in instance_array, ...
    (microsoft.public.excel.worksheet.functions)