Re: How to add elements to a JList without preventing refresh
- From: "Anonymous user" <Number_42@xxxxxxxxxxxx>
- Date: 30 Dec 2005 05:56:37 -0800
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 ;)
.
- Follow-Ups:
- Re: How to add elements to a JList without preventing refresh
- From: Thomas Hawtin
- Re: How to add elements to a JList without preventing refresh
- From: Rhino
- Re: How to add elements to a JList without preventing refresh
- References:
- How to add elements to a JList without preventing refresh
- From: Anonymous user
- Re: How to add elements to a JList without preventing refresh
- From: Thomas Hawtin
- How to add elements to a JList without preventing refresh
- Prev by Date: Re: How to add elements to a JList without preventing refresh
- Next by Date: Re: Newbie Problem with Java Label
- Previous by thread: Re: How to add elements to a JList without preventing refresh
- Next by thread: Re: How to add elements to a JList without preventing refresh
- Index(es):
Relevant Pages
|