Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

updating JList

Posted on 1998-11-19
3
Medium Priority
?
348 Views
Last Modified: 2011-04-14
I have a JList (in a scroll panel) and its model is a Vector.
the user can add to this vector elements and i can see them added to the list (by doing setModel(vector) and then repaint()), but if there are a lot of lines, the scrool panel does not work.
what can i do?
thank you!
0
Comment
Question by:rzvika3
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 60 total points
ID: 1227803
JList.setModel() does not always inform the UI part about the changes. you'd better
you DefaultListModel

DefaultListModel model = new DefaultListModel();
JList list = new JList(model);

now you'll manipulate the list through model's methods
addElementAt()
removeElementAt()
etc.

DefaultListModel will send all the needed notifications to the JListUI part for you.

if you have more specific problem, just post it here
0
 

Author Comment

by:rzvika3
ID: 1227804
thank you for your answer!
i knew about this thing (partly), but if i have the data in vector and i want to add to this vector and remove from it, how can i 'link' it to the list model so that it will get the vector at the start and every time i add an element, it will be added to the vector.

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227805
if you want to hold your data in your vector, you'd better implement your own ListModel.
you have to extend AbstractListModel and
implement those methods
  getElementAt(int)
  getSize()

whenever data in your model changes you should call one of these methods
 fireContentsChanged(Object, int, int)
 fireIntervalAdded(Object, int, int)
 fireIntervalRemoved(Object, int, int)

and AbstractListModel will inform the UI part about changes in the Model, UI part will
ask you about the new data and repaint itself.

So you are holding the data in your favourite Vector :), you can implement whatever methods you
need for modifing it and JList just paints it ...
(you can look at the sources of the DefaultListModel to see how it is implemented ...)

 heyhey
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question