当前位置: 代码迷 >> Java相关 >> JList问题
  详细解决方案

JList问题

热度:175   发布时间:2006-11-14 17:32:48.0
JList问题
mode1=new DefaultListModel();
jList1=new JList(mode1);
DefaultListModel jList1Model=(DefaultListModel)jList1.getModel();

jList1Model.addElement(new ImageIcon(picsonline[0])); //picsonline
//上面的方法向jList1中添加 图象 类型元素 (1)
// jList1Model.addElement("String类型");
//上面的方法向jList1中添加String类型元素 (2)
//jList1Model.addElement(new Object[]{"String类型",new ImageIcon(picsonline[0])});
//上面的方法尝试添加图象+String类型元素 (3)
结果(1)(2)正常, (3)显示为像是对象的地址似的一些乱码....

请帮忙.. 知道的告知一下吧 感激不禁....
或者提供一个其他的方法可以向JList的一个元素添加图象+文字(String)

谢了

搜索更多相关的解决方案: JList  new  ImageIcon  quot  String  

----------------解决方案--------------------------------------------------------
它默放的显示方式是字符串格式
如果你想显示别的什么方式,比如,显示图片,显示的字体颜色等的改变
请自己去实现ListCellRenderer
然后调用此JList的setCellRenderer(ListCellRenderer cellRenderer)方法,将你实现的绘制器做为此JList的绘制器
----------------解决方案--------------------------------------------------------
千里 你说的方法能实现动态更改嘛
??
----------------解决方案--------------------------------------------------------

加事件处理就可以


----------------解决方案--------------------------------------------------------
ListCellRenderer 我知道的好像用于初始化 JList可以
千里 能不能告诉我 我用你的方法 怎么实现类似JList中元素的 add() 和remove()的方法 我没有找到....

还是每次改变JList中的内容都要 setCellRender(new CellRenderer()) 来更新 ..

----------------解决方案--------------------------------------------------------
JList 里面的内容,你可以写一个Model
ListModel
----------------解决方案--------------------------------------------------------
JList的设计思想就是典型的MVC思想
Model负责保存数据,Renderer负责绘制图形
JList本身负责响应事件
----------------解决方案--------------------------------------------------------
我写了一个带图标的JComboBox.....
希望对你有用..
import javax.swing.*;
import java.awt.*;
import javax.swing.border.LineBorder;

public class IconRenderer extends JLabel implements ListCellRenderer{
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {

Object[] cell = (Object[]) value;
this.setIcon((Icon) cell[0]);
this.setText(cell[1].toString());
this.setToolTipText(cell[2].toString());
this.setBorder(new LineBorder(Color.white));
if(isSelected)
this.setForeground(Color.magenta);
else
this.setForeground(list.getForeground());

return this;
}
}



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class IconComboBoxDemo extends JFrame{
private JComboBox iconComboBox;

public IconComboBoxDemo(){
super("带图标的ComboBox");

Object[][] obj = {{new ImageIcon("1.gif"),"aaaa","aaaaa"},
{new ImageIcon("2.gif"),"cccc","bbbbb"},
{new ImageIcon("3.gif"),"adfefa","aetggf"}
};
iconComboBox = new JComboBox();
iconComboBox.setMaximumRowCount(3);
iconComboBox.setRenderer(new IconRenderer());

for(int i =0;i<obj.length;i++){
iconComboBox.addItem(obj[i]);
}

this.getContentPane().add(iconComboBox,BorderLayout.NORTH);

this.setSize(300,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args){
new IconComboBoxDemo();
}
}



----------------解决方案--------------------------------------------------------

好的 谢谢 一会回来试试 下午还有课
谢谢大家了


----------------解决方案--------------------------------------------------------

ok搞定了

多谢 千里的提示
JList的设计思想就是典型的MVC思想
Model负责保存数据,Renderer负责绘制图形 // 就是这个方法
JList本身负责响应事件


----------------解决方案--------------------------------------------------------