[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

JScrollPane will not disappear when not needed anymore

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?
0
Suppai
Asked:
Suppai
  • 5
  • 5
  • 4
2 Solutions
 
objectsCommented:
how are you removing elements? You need to make sure you do it from event dispatch thread

you should not need to repaint()

0
 
SuppaiAuthor 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?
0
 
objectsCommented:
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)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
objectsCommented:
You should create a DefaultListModel and use that with your list

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

0
 
SuppaiAuthor 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.
0
 
objectsCommented:
> 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.

0
 
CEHJCommented:
It would be good to post your code, including where you originally add the component to the scroll pane
0
 
SuppaiAuthor 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

0
 
CEHJCommented:
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

0
 
SuppaiAuthor 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?
0
 
CEHJCommented:
You can use the DLM directly, yes. It's Serializable
0
 
CEHJCommented:
The question is though: is it going to fix your GUI problem? Let us know
0
 
SuppaiAuthor 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.
0
 
CEHJCommented:
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now