开发者

Elements not adding to JList

开发者 https://www.devze.com 2022-12-12 13:45 出处:网络
For some reason, I can\'开发者_开发知识库t add anything to my JList. The JList is visible but simply shows white - nothing can be selected.

For some reason, I can'开发者_开发知识库t add anything to my JList. The JList is visible but simply shows white - nothing can be selected.

List list;
DefaultListModel listModel;
//...
list = new JList();
list.setBounds(220,20,150,200);
listModel = new DefaultListModel();
listModel.addElement("ONE");
panel.add(list);

Am I missing something?


The JList is not using the listModel.

One way is to initialize the JList by specifying a ListModel to use:

DefaultListModel listModel = ...
JList list = new JList(listModel);

Then, performing changes to the listModel (such as calling addElement) will cause the changes to appear on the JList.

For more information on using JLists, the How to Use Lists lesson from The Java Tutorials is a good source.


You never set the list's model to the ListModel you've constructed.

0

精彩评论

暂无评论...
验证码 换一张
取 消