Solved

updating JList

Posted on 1998-11-19
3
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is there a simpler dropbox system? 10 53
glassfish admin console 1 53
jmeter usage 4 68
listing all the respondents to a twitter feed - Java 5 49
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

710 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