Solved

updating JList

Posted on 1998-11-19
3
341 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 20 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services spring boot application error 3 183
HashTable highest marks enumeration alternative 9 37
MySQL  on Tomcat 8 43
throw exception 21 43
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 …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

786 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