用Swing开发GUI程序(四)JList

来源:互联网 发布:言论自由 知乎 编辑:程序博客网 时间:2024/06/11 09:25

JList是客户端还算比较常见的组件,偶尔也用得到,值得一学。

从API可以看出,JList的构造方法有四种
这里写图片描述

// 创建一个Jlist来显示 String数组 String[] data = {"one", "two", "three", "four"}; JList myList = new JList(data);

可通过构造方法或 setModel 方法向 JList 直接提供 ListModel。内容不需要是静态的,即项数和项值可以随时间而更改。

简单的、动态内容的 JList 应用程序可以使用 DefaultListModel 类维护列表元素。此类实现 ListModel 接口

侦听列表选择中更改的首选方法是向 JList 中直接添加 ListSelectionListener。然后,JList 负责侦听选择模型并向侦听器通知更改。

绘制 JList 中的单元由称为单元渲染器(以 cellRenderer 属性的形式安装在列表上)的委托进行处理。渲染器提供一个其用法类似 “rubber stamp” 的 java.awt.Component 来绘制单元。每当需要绘制单元时,列表的 ListUI 就请求组件的单元渲染器,将其移动到位,然后通过其 paint 方法绘制单元的内容。默认单元渲染器(它使用 JLabel 组件呈现)由列表的 ListUI 安装。用户还可以使用如下代码替换自己的渲染器:

  // Display an icon and a string for each object in the list. class MyCellRenderer extends JLabel implements ListCellRenderer {     final static ImageIcon longIcon = new ImageIcon("long.gif");     final static ImageIcon shortIcon = new ImageIcon("short.gif");     // This is the only method defined by ListCellRenderer.     // We just reconfigure the JLabel each time we're called.     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() > 10) ? longIcon : shortIcon);         if (isSelected) {             setBackground(list.getSelectionBackground());             setForeground(list.getSelectionForeground());         } else {             setBackground(list.getBackground());             setForeground(list.getForeground());         }         setEnabled(list.isEnabled());         setFont(list.getFont());         setOpaque(true);         return this;     } } myList.setCellRenderer(new MyCellRenderer());

JList 不实现直接滚动。要创建一个滚动的列表,请将它作为 JScrollPane 的视口视图。例如:

 JScrollPane scrollPane = new JScrollPane(myList); // Or in two steps: JScrollPane scrollPane = new JScrollPane(); scrollPane.getViewport().setView(myList);

JList 没有提供两次或三次(或 N 次)鼠标单击的任何特殊处理,但是,如果希望对这些事件采取操作,则可以很方便地添加一个 MouseListener。使用 locationToIndex 方法确定单击的是哪一个单元。例如:

 MouseListener mouseListener = new MouseAdapter() {     public void mouseClicked(MouseEvent e) {         if (e.getClickCount() == 2) {             int index = list.locationToIndex(e.getPoint());             System.out.println("Double clicked on Item " + index);          }     } }; list.addMouseListener(mouseListener);

下面介绍一种我个人很喜欢的一种样式

public static DefaultListModel lItems = new DefaultListModel();private JList list = new JList(lItems);private JScrollPane jsp = new JScrollPane(list);list.setCellRenderer(new MyRender());//MyRender是重写的一个JLIST样式private class MyRender extends JButton implements ListCellRenderer {        public MyRender() {            this.setOpaque(false);        }        @Override        //by vc        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {            setText(value.toString());            Color background = Color.WHITE;            Color foreground = Color.BLACK;            if (isSelected) {                background = Color.green;                foreground = Color.red;            }            setBackground(background);            setForeground(foreground);            return this;        }    }

这里写图片描述

JList API传送门
http://tool.oschina.net/uploads/apidocs/jdk-zh/javax/swing/JList.html

原创粉丝点击