We help IT Professionals succeed at work.

JScrollPane will not disappear when not needed anymore

Suppai
Suppai asked
on
Medium Priority
406 Views
Last Modified: 2013-11-23
I have a JList with items complete with a JScrollPane set with JScrollPane set with ScrollBar Policies VERTICAL_SCROLLBAR_AS_NEEDED and HORIZONTAL_SCROLLBAR_AS_NEEDED repspectively. However when I clear items from the list and do a repaint so that the scrollbars arent needed anymore they are not disappearing automatically as one would expect when using "As needed". How do I make them disappear when not needed?
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
how are you removing elements? You need to make sure you do it from event dispatch thread

you should not need to repaint()

Author

Commented:
I'm simply removing the items from the Vector<String> used to define the data in the list and then repainting, which works fine besides the problem I mentioned. I tried to use a DefaultListModel at first, but the cassting from JList.getModel to DefaultListModel kept causing problems so I stuck with the Vector<String> implementation. I'm quite new to Java so I'm not familiar with the event dispatch thread, can you elaborate?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
thats not inforing the gui that they are removed.
you need to remove them from the list (or fire eent after removing them from vector)
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
You should create a DefaultListModel and use that with your list

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
So removing them from the list using a ListModel instead of using the Vector<String> underlying data method? How do I fire and event in order to refresh the JList with the data currently stored in the Vector? I dont get that the gui is not informed of the items being removed,because when removing a few items and repainting the gui is updated accordinlgy. The only thin not being updated accordingly is the scrollpane.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> So removing them from the list using a ListModel instead of using the Vector<String> underlying data method? How do I fire and event in order to refresh the JList with the data currently stored in the Vector?

You need to reset the list data:
list.setData(myVector);

> I dont get that the gui is not informed of the items being removed

Evctors have no knowledge of how they are being used so cannot notify of changes.
ListModels do

> ,because when removing a few items and repainting the gui is updated accordinlgy. The only thin not being updated accordingly is the scrollpane.

which means it is *not* being updated accordingly.
Its only removing the items because you (unecesarily)  force it to repaint.

CERTIFIED EXPERT
Top Expert 2016

Commented:
It would be good to post your code, including where you originally add the component to the scroll pane

Author

Commented:
Here is some code bits, maybe more confusing than useful as theyre taken out of their original context. However I get that the point is that when using a Vector<String> and simply editing in that and repainting when the list is manipulated in the gui is the worng way to do it. However I had a hard time figuring out how to use the DefaultListModel for this purpose instead. Especially because the cast from Jlist.getModel() to DefaultListModel failed although i replicated an example from a website. So how can I use the ListModels? I find them quite confusing, whereas the Vector is a straightforward container. A little extra info: when resizing the frame where the lists are contained the scrollbar is updated as it should be. Is there a way to perhaps invoke the method updating the frame manually? Or should this be done in another fashion? I might as well learn to do it the right way.
//the init of the scrollpane with the list (listComponent variable)
this.listComponent = getListComponent();
		JScrollPane scroller = new JScrollPane(listComponent);
		scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
 
//The internal function creating the JList component, items variable is a Vector<String> object with the list items.
private JList getListComponent(){		
		JList listComponent = new JList(items);
		listComponent = new JList(items);				
		listComponent.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		listComponent.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		listComponent.setLayoutOrientation(JList.VERTICAL);		
		return listComponent;
	}
 
//This is the code for item removal from the list
public void removeSelected(){
		for (int i = listComponent.getSelectedIndices().length-1; i > -1 ; i--) {
			items.removeElementAt(listComponent.getSelectedIndices()[i]);
		}		
		this.repaint();
	}

Open in new window

CERTIFIED EXPERT
Top Expert 2016
Commented:
DefaultListModel is horribly broken but you can create one as below. Let me know how you get on with a proper DLM:
DefaultListModel dlm = new DefaultListModel();
for(Object o : yourVector) {
	dlm.addElement(o);
}

Open in new window

Author

Commented:
Yeah looking at the methods first hand I really couldnt see the way around add one-by-one loop, but it was nice to get it confirmed by an expert. I find it kinda weird that theres no support for adding an whole array or Collection. One more qestion: can i replace the Vectors in my code (not directly ofcourse) with the DefaultListModel? The vectors are atm only used as containers of JList Strings. However theyre are part of a serializable object serialized and saved/read from a file. Is this possible with the DLM? I dont see it implementing the Serializable interface. If not I guess I wont get around vector->DLM and DLM->vector conversion methods?
CERTIFIED EXPERT
Top Expert 2016

Commented:
You can use the DLM directly, yes. It's Serializable
CERTIFIED EXPERT
Top Expert 2016

Commented:
The question is though: is it going to fix your GUI problem? Let us know

Author

Commented:
Oh yeah it is, was looking at another object in the API documentation. I have rewritten my code so that it now uses the DefaultListModel instead of the Vector and it works like charm. Solved the ScrollPane problem and furthermore solved another update problem when adding items to the lidt via the gui. Thanks alot.
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.