//Test.java
import java.awt.*;
import java.awt.event.*;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.*;
import javax.swing.table.*;
public class Test extends JFrame {
public static void main(String args[]) {
SwingApp.launch(new Test(), "排序修饰者",
300, 300, 450, 250);
}
public Test() {
// 生成修饰者的实例,该实例用于修饰Swing Table原有的表模型
// 该实例必须是final的,因为它会被内嵌类引用。
final TableSortDecorator decorator =
new TableBubbleSortDecorator(table.getModel());
// 将表的模型设定为修饰者。因为修饰者实现了TableModel接口,
// 因此Swing Table对象不知道修饰者和真实对象之间的差别。
table.setModel(decorator);
getContentPane().add(new JScrollPane(table),
BorderLayout.CENTER);
// 在界面中添加一个状态区
getContentPane().add(SwingApp.getStatusArea(),
BorderLayout.SOUTH);
SwingApp.showStatus("进行排序前");
// 获得对表中列头的引用。
JTableHeader hdr = (JTableHeader)table.getTableHeader();
// 当单击鼠标单击列头时,调用修饰者的sort()方法。
hdr.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
TableColumnModel tcm = table.getColumnModel();
int vc = tcm.getColumnIndexAtX(e.getX());
int mc = table.convertColumnIndexToModel(vc);
// 进行排序
decorator.sort(mc);
// 更新状态区
SwingApp.showStatus(headers[mc] + " 排序中");
}
});
}
final String[] headers = { "品名", "价格/每斤." };
JTable table = new JTable(new Object[][] {
{"苹果", "1.2"}, {"芒果", "4"},
{"柠檬", "2.5"},{"香蕉", "0.8"},
{"桔子", "1.8"}, {"西瓜", "0.5"},
{"橘子", "2.5"}, {"樱桃", "3.6"},
{"柚子", "0.8"}, {"葡萄", "2.2"},
}, headers);
}
class SwingApp extends WindowAdapter {
private SwingApp() {} // 该类不能被初始化
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
launch(f,title,x,y,w,h,null);
}
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h,
String propertiesFilename) {
statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT);
if(propertiesFilename != null) {
resources = ResourceBundle.getBundle(
propertiesFilename, Locale.getDefault());
}
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
static public JPanel getStatusArea() {
return statusArea;
}
static public void showStatus(String s) {
status.setText(s);
}
static Object getResource(String key) {
if(resources != null) {
return resources.getString(key);
}
return null;
}
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");
static private ResourceBundle resources;
}
在上面的代码中,排序修饰者修饰了Swing表原有的模型。当用户在列头上单击鼠标的时候,程序调用修饰者的sort()方法。在研究排序修饰者的代码前,然我们来看一下图7中的类结构图: