简体   繁体   English

从DefaultListModel绑定的Jlist是否可以仅显示某些元素?

[英]Jlist binded from DefaultListModel is it possible to Display Only Certain elements ?

Is it possible to bind or show only some of the ListModel or List When i Bind it to a Jlist item ? 当我将其绑定到Jlist项目时,是否可以仅绑定或显示某些ListModel或List?

In my Example I have a list model made from an array of a class(pizza) which has multiple variable's I only want to show one, but they are all being displayed in a line. 在我的示例中,我有一个由具有多个变量的类(比萨)数组构成的列表模型,我只想显示一个变量,但是它们都显示在一行中。

  listModel = new DefaultListModel();
  listModel = ListHandler.populateListModel(listModel); 
  // This returns listModel binded from array of a class<Pizza>

  list = new JList(listModel);
  list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  list.setSelectedIndex(0);
  JScrollPane listScrollPane = new JScrollPane(list);
  Apanel.add(listScrollPane, BorderLayout.CENTER);

This code when then display all the items in Pizza per line, Is it possible to just show 1 varible from pizza ? 此代码然后每行显示Pizza中的所有项目时,是否可能仅显示Pizza中的1个变量? Cheese for example in the listbox alone... ? 例如仅在列表框中的奶酪...?

The default cell renderer is calling toString() on the value objects in your model. 默认的单元格渲染器在模型中的值对象上调用toString() You can do either of these: 您可以执行以下任一操作:

  • Override/change the value returned by toString() to be the text you want to show 覆盖/更改由toString()返回的值,使其成为您要显示的文本
  • Change the cell renderer to format the value object differently from the default 更改单元格渲染器以将值对象的格式设置为与默认格式不同

I'd prefer the second, since you might want toString() to have a specific other value for some other reason. 我希望使用第二种方法,因为出于某些其他原因,您可能希望toString()具有特定的其他值。

See http://docs.oracle.com/javase/7/docs/api/javax/swing/ListCellRenderer.html 参见http://docs.oracle.com/javase/7/docs/api/javax/swing/ListCellRenderer.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM