以前对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架构,知道这一点,对于其他组件的学习就容易多了。
相关推荐
构造函数中传递String数组给JList添加内容,同时给JList添加滚动条。
jlist应用例子,JLIST应用例子,移动举例
使用JList 显示图片并动态改变显示的图片!
NULL 博文链接:https://lisaiyu.iteye.com/blog/783135
JLIst的使用,主要是介绍基础
关于java中jlist的一些用法,。。
文档中介绍了JList控件的简单使用方法,包括对列表点击事件的处理。
JList多选值的获取, 由JList组件实现的列表框有3种选取模式,设置方法为通过JList类的setSelectionMode(int selectionMode)方法,该方法的入口参数可以通过ListSelectionModel类中的静态常量设置。
JList 上下左右拖动,例子说明.上下左右拖动
java构建几种个性的JList,可以用图片,可以拖动JList控件
JList事件监听器方法实现代码
swing中的jlist 代码中定义了两个jlist 从一个jlist双击列表项可以将该项从这个列表删除并添加至另外一个列表中去
自定义的JList,像android的ListView,可以点击按钮后马上删除当前条,并即时显示最新数据,有点数据驱动UI的感觉。Swing自定义控件必备。
javaswing更酷的Jlist.pdf
swing JList 拖拽记录到 JTable
JList是Swing库提供的一个用于显示列表数据的组件,它可以显示单个或多个项目,并且允许用户进行选择操作。
实现Jlist带上了图片标签 主要用上了ListCellRenderer
重新实现了CellRenderer,可同时显示图片和文字
JFC Swing活学活用系列之定制JList显示.rar
JList在点击空白处时候,会自动将最后一项作为选中项,这个对于界面的外观,特别是当你有右击菜单时候,会比较麻烦,这个问题也困扰我一上午,后来,经过查阅,在国外的一个论坛问答中找到了答案,下面是解决问题...