java - jList - 添加元素并显示字符串?

标签 java swing list compiler-errors jlist

怎么了?

我在我的项目中创建了一个无法检索元素的 jList。我知道 jList 只接受对象,但我将字符串添加到我的列表中,因为当我添加“Discipline”对象时,我看到类似“Discipline{id=21, name=DisciplineName} ”在我看来。所以,我添加的是字符串而不是对象。

以下是我的代码:

ArrayList<Discipline> query = myController.select();
for (Discipline temp : query){
    model.addElement(temp.getNome());
} 

当我获得一个元素中双击的索引时,我尝试检索我的字符串以进行查询并了解这是什么规则。但是我遇到了一些错误,看看我已经尝试过什么:

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

我真的不知道“final”是什么意思,但是我能做些什么来解决这个问题呢?我认为的一件事是:

我可以添加一个 Discipline 而不是 String,向用户显示 discipline.getName() 并检索 Discipline 对象吗?

最佳答案

是的,添加 Discipline 对象。一个快速的解决方法是更改​​ Discipline 的 toString 方法,但更好的解决方法是创建一个 ListCellRenderer 以一个漂亮的字符串显示每个 Discipline 的数据。

这是我在我的一个项目中使用的两个 ListCellRenderer,用于将 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);
  }      
}

它们是这样声明的:

private ListCellRenderer imgRenderer = new ImgListCellRenderer();
private ListCellRenderer nonImgRenderer = new NonImgCellRenderer();

我这样使用它们:

  imgList.setCellRenderer(imgRenderer);

DefaultListCellRenderer 非常强大,知道如何正确显示 String 或 ImageIcon(因为它基于 JLabel)。

关于java - jList - 添加元素并显示字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17135357/

相关文章:

java - 从字符串到字节数组,然后再到字符串

java - float 操作按钮滚动奇怪的行为

java - 如何处理 JOptionPane 中的取消按钮

java - JSF 添加到列表中的 selectOneMenu<School>

java - Jlabel 不随鼠标移动监听器移动

java - 惰性初始化 ="true"。它有什么作用?为什么我无法从 Swing 应用程序获取 Spring bean,而无需将lazy-init 设置为 true?

html - 如何处理我的导航栏以使其看起来像图片中的那样?

list - F# 将在 for 循环中创建的元素添加到列表

python - 如何将正负十进制数列表规范化到特定范围

java - 除以零而不出现 ArithmeticException