第17章 列表-阿里云开发者社区

开发者社区> dongzhumao> 正文

第17章 列表

简介: 第17章 列表   由JList类代表的Swing列表显示一个可选取对象列表,它支持三种选取模式:单选取、单间隔选取和多间隔选取。  JList类把维护和绘制列表的工作委托给一个对象来完成。
+关注继续查看

17章 列表

  由JList类代表的Swing列表显示一个可选取对象列表,它支持三种选取模式:单选取、单间隔选取和多间隔选取。
  JList类把维护和绘制列表的工作委托给一个对象来完成。一个列表的模型维护一个对象列表,列表单元绘制器将这些对象绘制在列表单元中。
  缺省情况下,列表单元绘制器是DefaultListCellRenderrer的实例,它绘制表17-1中列出的对象。图标和字符串按原样显示,而所有其他类型对象的绘制方式是:通过显示从这些对象的toString返回的字符串来绘制这些对象。

17-1 一个简单的列表样例

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

public class Test extends JApplet {
public void init() {
Container contentPane = getContentPane();
Object[] items = { "item one", "item two", "item three",
"item four", "item five", "item six",
"item seven", "item eight",
"item nine", "item ten" };

JList list = new JList(items);
JScrollPane sp = new JScrollPane(list);

list.setVisibleRowCount(7);

contentPane.setLayout(new FlowLayout());
contentPane.add(sp);
}
}

17.1 列表模型

 

17-2 一个带有缺省列表模型的列表

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

public class Test extends JApplet {
private JList list = new JList();

String[] items = { "item[0]", "item[1]", "item[2]",
"item[3]", "item[4]", "item[5]",
"item[6]", "item[7]",
"item[8]", "item[9]" };

public void init() {
Container contentPane = getContentPane();
JPanel controlPanel = new ControlPanel(list);

contentPane.add(controlPanel, BorderLayout.NORTH);
contentPane.add(new JScrollPane(list),
BorderLayout.CENTER);
populateList();
}
public void populateList() {
DefaultListModel model = new DefaultListModel();

for(int i=0; i < items.length; ++i)
model.addElement(items[i]);

list.setModel(model);
}
}
class ControlPanel extends JPanel {
JButton remove = new JButton("remove selected items");
JButton add = new JButton("add item");

public ControlPanel(final JList list) {
add(remove);
add(add);

remove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int[] selected = list.getSelectedIndices();
DefaultListModel model =
(DefaultListModel)list.getModel();

for(int i=0; i < selected.length; ++i) {
model.removeElementAt(selected[i] - i);
}
}
});
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final DefaultListModel model =
(DefaultListModel)list.getModel();

String s = JOptionPane.showInputDialog(
list,
"Enter item text:");

model.addElement(s);

SwingUtilities.invokeLater(new Runnable() {
public void run() {
list.ensureIndexIsVisible(
model.getSize()-1);
}
});
}
});
}
}

17.1.1 AbstractListModel

 

17.1.2 DefaultListModel

 

17.2 列表选取

 

17.3 列表单绘制器

 

17-3 实现一个定制列表单元绘制器

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

public class Test extends JApplet {
private String[] names = new String[] {
"baseball player", "basketball player",
"beach player", "chef",
"hockey player", "software developer",
"construction worker", "martial artist",
"soccer", "movie star"
};
private String[] pics = new String[] {
"baseball.gif", "basketball.gif",
"beach_umbrella.gif", "dining.gif",
"hockey.gif", "mad_hacker.gif",
"men_at_work.gif", "punch.gif",
"soccer.gif", "filmstrip.gif"
};

public void init() {
Container contentPane = getContentPane();
ListModel model =
new NameAndPictureListModel(names, pics);

ListCellRenderer renderer =
new NameAndPictureListCellRenderer();

JList list = new JList(model);

list.setCellRenderer(renderer);
list.setVisibleRowCount(5);

contentPane.setLayout(new FlowLayout());
contentPane.add(new JScrollPane(list));
}
}
class NameAndPictureListModel extends DefaultListModel {
public NameAndPictureListModel(String[] names,String[] pics) {
for(int i=0; i < names.length; ++i) {
addElement(new Object[] {
names[i], new ImageIcon(pics[i]) } );
}
}
public String getName(Object object) {
Object[] array = (Object[])object;
return (String)array[0];
}
public Icon getIcon(Object object) {
Object[] array = (Object[])object;
return (Icon)array[1];
}
}
class NameAndPictureListCellRenderer extends JLabel
implements ListCellRenderer {
private Border
lineBorder = BorderFactory.createLineBorder(Color.red, 2),
emptyBorder = BorderFactory.createEmptyBorder(2,2,2,2);

public NameAndPictureListCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
NameAndPictureListModel model =
(NameAndPictureListModel)list.getModel();

setText(model.getName(value));
setIcon(model.getIcon(value));

if(isSelected) {
setForeground(list.getSelectionForeground());
setBackground(list.getSelectionBackground());
}
else {
setForeground(list.getForeground());
setBackground(list.getBackground());
}

if(cellHasFocus) setBorder(lineBorder);
else setBorder(emptyBorder);

return this;
}
}

17.3.1 JList属性

 

17.3.2 JList事件

 

17-4 检测列表选取的调整值

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

public class Test extends JApplet {
public void init() {
Container contentPane = getContentPane();

String[] items = { "item[0]", "item[1]", "item[2]",
"item[3]", "item[4]", "item[5]",
"item[6]", "item[7]",
"item[8]", "item[9]" };

JList list = new JList(items);

contentPane.add(new JScrollPane(list),
BorderLayout.CENTER);

list.addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
String s;

if(e.getValueIsAdjusting()) {
s = "adjusting selection ...";
}
else {
s = "selection from " + e.getFirstIndex() +
" to " + e.getLastIndex();
}
showStatus(s);
}
});
}
}

17-5 处理列表选取事件

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

public class Test extends JApplet {
private ControlPanel controlPanel;

public void init() {
Container contentPane = getContentPane();
JPanel listPanel = new JPanel();

String[] items = { "item[0]", "item[1]", "item[2]",
"item[3]", "item[4]", "item[5]",
"item[6]", "item[7]",
"item[8]", "item[9]" };

JList list = new JList(items);

list.setPrototypeCellValue("MMMMMMM");

controlPanel = new ControlPanel(list);
controlPanel.update();

listPanel.setBorder(BorderFactory.createEtchedBorder());
listPanel.add(new JScrollPane(list));

contentPane.add(controlPanel, BorderLayout.NORTH);
contentPane.add(listPanel, BorderLayout.CENTER);

list.addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
controlPanel.update();
}
});
}
}
class ControlPanel extends JPanel {
private JComboBox mode = new JComboBox();
private JButton clear = new JButton("clear selection");

private String single = "SINGLE_SELECTION",
singleInterval = "SINGLE_INTERVAL_SELECTION",
multipleInterval = "MULTIPLE_INTERVAL_SELECTION";

private JLabel leadLabel = new JLabel(),
anchorLabel = new JLabel(),
minLabel = new JLabel(),
maxLabel = new JLabel(),
selIndicesLabel = new JLabel();

private JList list;

public ControlPanel(JList l) {
JPanel top = new JPanel(),
mid = new JPanel(),
bottom = new JPanel();

this.list = l;

setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBorder(BorderFactory.createEtchedBorder());

top.add(mode);
top.add(clear);

mid.add(new JLabel("Lead:")); mid.add(leadLabel);
mid.add(new JLabel("Anchor:")); mid.add(anchorLabel);
mid.add(new JLabel("Minimum:")); mid.add(minLabel);
mid.add(new JLabel("Maximum:")); mid.add(maxLabel);

add(top);
add(mid);
add(bottom);

mode.addItem(single);
mode.addItem(singleInterval);
mode.addItem(multipleInterval);
initializeSelectionMode();

bottom.add(new JLabel("Selected Indices:"));
bottom.add(selIndicesLabel);

mode.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED)
setSelectionMode((String)e.getItem());
}
});
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
list.clearSelection();
}
});
}
public void update() {
int lead = list.getLeadSelectionIndex(),
min = list.getMinSelectionIndex(),
max = list.getMaxSelectionIndex(),
anchor = list.getAnchorSelectionIndex();

leadLabel.setText(Integer.toString(lead) + " / ");
anchorLabel.setText(Integer.toString(anchor) + " / ");
minLabel.setText(Integer.toString(min) + " / ");
maxLabel.setText(Integer.toString(max) + " / ");

int[] selected = list.getSelectedIndices();
String s = new String();

for(int i = 0; i < selected.length; ++i) {
s += Integer.toString(selected[i]);

if(i < selected.length-1)
s += ",";
}
selIndicesLabel.setText(s);
validate();
}
private void initializeSelectionMode() {
int m = list.getSelectionMode();

switch(m) {
case ListSelectionModel.SINGLE_SELECTION:
mode.setSelectedItem(single);
break;
case ListSelectionModel.SINGLE_INTERVAL_SELECTION:
mode.setSelectedItem(singleInterval);
break;
case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:
mode.setSelectedItem(multipleInterval);
break;
}
}
private void setSelectionMode(String s) {
if(s.equals("SINGLE_SELECTION")) {
list.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
}
else if(s.equals("SINGLE_INTERVAL_SELECTION")) {
list.setSelectionMode(
ListSelectionModel.SINGLE_INTERVAL_SELECTION);
}
else if(s.equals("MULTIPLE_INTERVAL_SELECTION")) {
list.setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
}
}

17-6 处理列表数据事件

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

public class Test extends JApplet {
private JList list = new JList();

private String[] items = {
"item one", "item two", "item three",
"item four", "item five", "item six",
"item seven", "item eight",
"item nine", "item ten"
};

public void init() {
Container contentPane = getContentPane();

JPanel controlPanel = new ControlPanel(this, list);

contentPane.add(controlPanel, BorderLayout.NORTH);
contentPane.add(new JScrollPane(list),
BorderLayout.CENTER);
populateList();
}
public void populateList() {
final DefaultListModel model = new DefaultListModel();

for(int i=0; i < items.length; ++i)
model.addElement(items[i]);

list.setModel(model);

if(list.isShowing())
list.revalidate();

model.addListDataListener(new ListDataListener() {
public void contentsChanged(ListDataEvent e) {
showStatus("contents changed");
}
public void intervalRemoved(ListDataEvent e) {
Object[] message = new Object[] {
"Removed item at index " + e.getIndex0(),
" ",
"There are now " + model.getSize() + " items"
};
JOptionPane.showMessageDialog(Test.this,
message,
"Items Removed", // title
JOptionPane.INFORMATION_MESSAGE); // type
}
public void intervalAdded(ListDataEvent e) {
showStatus("interval added");
}
});
}
}
class ControlPanel extends JPanel {
JButton remove = new JButton("remove selected items");
JButton repopulate = new JButton("repopulate");

public ControlPanel(final Test applet, final JList list) {
add(remove);
add(repopulate);

remove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int[] selected = list.getSelectedIndices();
DefaultListModel model =
(DefaultListModel)list.getModel();

for(int i=0; i < selected.length; ++i) {
model.removeElementAt(selected[i] - i);
}
}
});
repopulate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
applet.populateList();
}
});
}
}

 

 

17-7 处理鼠标双击和三击

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

public class Test extends JApplet {
public void init() {
Container contentPane = getContentPane();

String[] items = { "item one", "item two", "item three",
"item four", "item five", "item six",
"item seven", "item eight",
"item nine", "item ten" };

JList list = new JList(items);

contentPane.setLayout(new FlowLayout());
contentPane.add(new JScrollPane(list));

list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JList theList = (JList)e.getSource();
ListModel model = theList.getModel();

int index = theList.locationToIndex(e.getPoint());
String itemString =
(String)model.getElementAt(index);

String s = new String(" for " +
model.getElementAt(index));

switch(e.getClickCount()) {
case 1:
showStatus("Single Click" + s);
break;
case 2:
showStatus("Double Click" + s);
break;
case 3:
showStatus("Triple Click" + s);
break;
}
}
});
}
}

17.3.3 JList类总结

 

17.3.4 AWT兼容

 

17.4 本章回顾

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
使用NAT网关轻松为单台云服务器设置多个公网IP
在应用中,有时会遇到用户询问如何使单台云服务器具备多个公网IP的问题。 具体如何操作呢,有了NAT网关这个也不是难题。
22598 0
使用OpenApi弹性释放和设置云服务器ECS释放
云服务器ECS的一个重要特性就是按需创建资源。您可以在业务高峰期按需弹性的自定义规则进行资源创建,在完成业务计算的时候释放资源。本篇将提供几个Tips帮助您更加容易和自动化的完成云服务器的释放和弹性设置。
7757 0
windows server 2008阿里云ECS服务器安全设置
最近我们Sinesafe安全公司在为客户使用阿里云ecs服务器做安全的过程中,发现服务器基础安全性都没有做。为了为站长们提供更加有效的安全基础解决方案,我们Sinesafe将对阿里云服务器win2008 系统进行基础安全部署实战过程! 比较重要的几部分 1.
5457 0
阿里云服务器安全组设置内网互通的方法
虽然0.0.0.0/0使用非常方便,但是发现很多同学使用它来做内网互通,这是有安全风险的,实例有可能会在经典网络被内网IP访问到。下面介绍一下四种安全的内网互联设置方法。 购买前请先:领取阿里云幸运券,有很多优惠,可到下文中领取。
9425 0
阿里云服务器远程登录用户名和密码的查询方法
阿里云服务器远程连接登录用户名和密码在哪查看?阿里云服务器默认密码是什么?云服务器系统不同默认用户名不同
443 0
腾讯云服务器 设置ngxin + fastdfs +tomcat 开机自启动
在tomcat中新建一个可以启动的 .sh 脚本文件 /usr/local/tomcat7/bin/ export JAVA_HOME=/usr/local/java/jdk7 export PATH=$JAVA_HOME/bin/:$PATH export CLASSPATH=.
2140 0
阿里云ECS云服务器初始化设置教程方法
阿里云ECS云服务器初始化是指将云服务器系统恢复到最初状态的过程,阿里云的服务器初始化是通过更换系统盘来实现的,是免费的,阿里云百科网分享服务器初始化教程: 服务器初始化教程方法 本文的服务器初始化是指将ECS云服务器系统恢复到最初状态,服务器中的数据也会被清空,所以初始化之前一定要先备份好。
3227 0
+关注
dongzhumao
专注JAVA语言,对大数据、BI有深入研究,擅长制作解决方案。 10年工作经验,致力于软件开发,深入互联网教育行业。
163
文章
1
问答
文章排行榜
最热
最新
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载