[英]jList - Add element and show String?
what's up? 这是怎么回事?
I created one jList on my project that I can't retrieve the element. 我在我的项目上创建了一个无法检索元素的jList。 I know jList only accepts objects, but I was adding Strings to my list, because when I add "Discipline" object, I see something like " Discipline{id=21, name=DisciplineName} " on my view.
我知道jList只接受对象,但是我将字符串添加到我的列表中,因为当我添加“Discipline”对象时,我在我的视图中看到类似“ Discipline {id = 21,name = DisciplineName} ”的内容。 So, I'm adding strings instead objects.
所以,我正在添加字符串而不是对象。
Following is my code: 以下是我的代码:
ArrayList<Discipline> query = myController.select();
for (Discipline temp : query){
model.addElement(temp.getNome());
}
When I get the index of a double click in one element, I try to retrieve my String to make a query and know what's this discipline. 当我在一个元素中获得双击的索引时,我尝试检索我的String以进行查询并知道这个规则是什么。 But I'm getting some errors, see what I already tried :
但是我遇到了一些错误, 看看我已经尝试过了 :
Object discipline = lista1.get(index);
// Error: local variable lista1 is accessed from within inner class; needs to be declared final
String nameDiscipline = (String) lista1.get(index);
// Error: local variable lista1 is accessed from within inner class; needs to be declared final
I really don't know what means "final", but what can I do to solve this problem? 我真的不知道“最终”是什么意思,但我该怎么做才能解决这个问题呢? One thing that I thinked is:
我想到的一件事是:
Yes, add Discipline objects. 是的,添加Discipline对象。 A quick fix is to change Discipline's toString method, but a much better fix is to create a ListCellRenderer that displays each Discipline's data in a nice String.
快速解决方法是更改Discipline的toString方法,但更好的解决方法是创建一个ListCellRenderer,以一个漂亮的String显示每个Discipline的数据。
Here are two ListCellRenderers that I have used in a project of mine to change the item displayed in my JList from text to an ImageIcon: 以下是我在我的项目中使用的两个ListCellRenderers,用于将我的JList中显示的项目从文本更改为ImageIcon:
private class ImgListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (value != null) {
BufferedImage img = ((SimpleTnWrapper) value).getTnImage();
value = new ImageIcon(img); // *** change value parameter to an ImageIcon
}
return super.getListCellRendererComponent(list, value, index,
isSelected, cellHasFocus);
}
}
private class NonImgCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// all this does is use the item held by the list, here value
// to extract a String that I want to display
if (value != null) {
SimpleTnWrapper simpleTn = (SimpleTnWrapper) value;
String displayString = simpleTn.getImgHref().getImgHref();
displayString = displayString.substring(displayString.lastIndexOf("/") + 1);
value = displayString; // change the value parameter to the String ******
}
return super.getListCellRendererComponent(list, value, index,
isSelected, cellHasFocus);
}
}
They are declared like so: 它们被声明如下:
private ListCellRenderer imgRenderer = new ImgListCellRenderer();
private ListCellRenderer nonImgRenderer = new NonImgCellRenderer();
And I use them thusly: 我这样使用它们:
imgList.setCellRenderer(imgRenderer);
The DefaultListCellRenderer is pretty powerful and knows how to display a String or an ImageIcon correctly (since it is based off of a JLabel). DefaultListCellRenderer非常强大,并且知道如何正确显示String或ImageIcon(因为它基于JLabel)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.