Solved

updating JList

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now