• 0

[JAVA] How do you populate a JList?


Question

5 answers to this question

Recommended Posts

  • 0

Best way would be to make your own ListModel.

ListModel bigData = new AbstractListModel() {

public int getSize() { return Short.MAX_VALUE; }

public Object getElementAt(int index) { return "Index " + index; }

};

JList bigDataList = new JList(bigData);

You can replace return "Index " + index; with any returning any type from any data storage type you wish, and remember to replace return Short.MAX_VALUE with the length of your data array.

Link to comment
Share on other sites

  • 0
hmmm isn't there like some way to just add the data into the list? =\

If you want to change JList dynamically, you have to use either AbstractListModel or DefaultListModel

what data structure are you using for the JList

Link to comment
Share on other sites

  • 0
public class sampleListModel extends AbstractListModel implements Observer{
  
	POW parser;
	Element component;
	Vector result;

	public unitsListModel(CellMLParser p, Element m){
  
  parser = p;
  component = m;
  result = parser.getVect(component);
  p.addObserver(this);
  
	}

	public int getSize(){

  return result.size();
  
	}

	public Object getElementAt(int n){
  
  
  Node node = (Node) result.get(n);
    
  return node;
	}

	public void update(Observable observable, Object object){
  
  result = parser.getVect(component);
  fireContentsChanged(this,0,getSize());
  
	}

}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.