以前对JList不大了解,只知道显示固定的数据,今天特地的看了一下JList的API,基本弄懂了JList的用法,可以实现JList的动态更新和图片加载。
这是主类
package jlist_v1;
import java.awt.BorderLayout;
public class MyJistText extends JFrame {
private JPanel contentPane;
private JLabel label;
private JList list;
private Vector<Object> dataVec;
private JList< Object> list2;
private int count=1;
private JComboBox comboBox;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyJistText frame = new MyJistText();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MyJistText() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.WEST);
String[] words= { "quick", "brown", "hungry", "wild"};
list = new JList(words);
list.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2){
//得到双击的item的index
int index = list.locationToIndex(e.getPoint());
System.out.println("Double clicked on Item " + index);
//实现list2的动态更新
dataVec.addElement(""+count++);
list2.setModel(new StringListModel(dataVec));
}
}
});
scrollPane.setViewportView(list);
//list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
// list.setVisibleRowCount(2);//设置可见的行数
// list.setCellRenderer(new FontCellRenderer());
list.setCellRenderer(new MyCellRenderer());
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
boolean flag=e.getValueIsAdjusting();
int last=e.getLastIndex();
if(flag){System.out.println("按下"+last);}
else{
System.out.println("松开"+last);
System.out.println(list.getSelectedValue());
label.setText(list.getSelectedValue().toString());
}
}
});
dataVec=new Vector<Object>();
dataVec.add("100");
list2=new JList<Object>();
list2.setModel(new StringListModel(dataVec));
JScrollPane scrollPane_1 = new JScrollPane(list2);
contentPane.add(scrollPane_1, BorderLayout.EAST);
label = new JLabel("");
contentPane.add(label, BorderLayout.SOUTH);
comboBox = new JComboBox(words);
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
System.out.println(comboBox.getItemAt(comboBox.getSelectedIndex()));
}
});
contentPane.add(comboBox, BorderLayout.NORTH);
}
}
要实现JList的动态更新,需要实现ListModel接口
class StringListModel implements ListModel
{
private Vector<Object> dataVec;
public StringListModel(Vector<Object> dataVec) {
super();
this.dataVec = dataVec;
}
@Override
public int getSize() {
return dataVec.size();
}
@Override
public Object getElementAt(int index) {
return dataVec.elementAt(index);
}
@Override
public void addListDataListener(ListDataListener l) {
}
@Override
public void removeListDataListener(ListDataListener l) {
}
}
要实现JList元素的特殊效果,如加载图片,背景颜色设置,需要实现ListCellRenderer接口
class MyCellRenderer extends JLabel implements ListCellRenderer {
final static ImageIcon longIcon = new ImageIcon("images/qq.png");
final static ImageIcon shortIcon = new ImageIcon("images/weixin.png");
public Component getListCellRendererComponent(
JList list, // the list
Object value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // does the cell have focus
{
String s = value.toString();
setText(s);
setIcon((s.length()>5) ? longIcon : shortIcon);
if (isSelected) {
setBackground(Color.CYAN);
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
return this;
}
}
这些在api中都有相应的范例,不做过多介绍,只不过,通过对JTABLE,JLIST和JCOMBOBOX的研究发现,其动态更新都需要实现相应的model,更改外观都需要实现相应的渲染器,这些组件都采用了MVC架构,知道这一点,对于其他组件的学习就容易多了。