Refresh JList in a JFrame

后端 未结 4 1912
闹比i
闹比i 2020-12-10 10:14

I\'ve got a JList which displays information from a vector. The user can then add and remove information from this vector. Is it possible to refresh the JList inside my JFra

相关标签:
4条回答
  • 2020-12-10 10:51

    You should not be updating the Vector. Changes should be made directly to the ListModel then the table will repaint itself automatically.

    If you decide to recreate the ListModel because of the changes to the Vector, then you update the list by doing:

    list.setModel( theNewModel );
    

    Edit: Forget the Vector and load the data directly into the DefaultListModel:

    DefaultListModel model = new DefaultListModel();
    model.addElement( "one" );
    model.addElement( "two" );
    JList list = new JList( model );
    

    Now whenever you need to change the data you update the model directly using the addElement(), removeElement() or set() methods. The list will automatically be repainted.

    0 讨论(0)
  • 2020-12-10 10:56

    Call updateUI on the Jlist after modifying your Vector.

    0 讨论(0)
  • 2020-12-10 10:56

    I think I found the solution for the Jlist's graphic 'refresh'. Try calling this method after each add or remove element of the model that is held by the Jlist.

    Jlist_name.ensureIndexIsVisible(model_name.getSize());

    0 讨论(0)
  • 2020-12-10 11:06

    you can use list.setListData(vector) each time the vector changes

    0 讨论(0)
提交回复
热议问题