Posted on 2010-08-24 11:52
TWaver 阅读(1085)
评论(0) 编辑 收藏
上篇的例子主要的动作在editor上,不过editor只有在点击在cell单元的时候才会显示,所以还不得不造一个界面跟editor一样的renderer进行“造假”,当然由于这个例子只有两个按钮,所以renderer的代码不多,不过如果editor的组件很多,而且经常需要改动那不就麻烦了,所以我们可以考虑一下让renderer复用editor的显示部分,以下的代码就是很好的说明了这种复用
1
import java.awt.*;
2
import java.awt.event.*;
3
import javax.swing.*;
4
import javax.swing.table.*;
5
6
public class TableTest2 extends JFrame
{
7
JTable table;
8
String[] states = new String[]
{"stop", "stop", "stop"};
9
10
// model
11
class TableModel extends AbstractTableModel
{
12
public int getColumnCount()
{
13
return 2;
14
}
15
public int getRowCount()
{
16
return states.length;
17
}
18
public Object getValueAt(int rowIndex, int columnIndex)
{
19
if(columnIndex == 0)
{
20
return states[rowIndex];
21
}
22
return null;
23
}
24
public String getColumnName(int columnIndex)
{
25
if(columnIndex == 0)
{
26
return "state";
27
}else
{
28
return "operate";
29
}
30
}
31
public boolean isCellEditable(int rowIndex, int columnIndex)
{
32
if(columnIndex == 0)
{
33
return false;
34
}else
{
35
return true;
36
}
37
}
38
}
39
40
// cell editor
41
class Editor extends AbstractCellEditor implements TableCellEditor, ActionListener
{
42
int row;
43
JTable table;
44
JPanel panel;
45
JButton start;
46
JButton stop;
47
Editor()
{
48
panel = new JPanel();
49
panel.setLayout(new GridLayout(1, 2));
50
start = new JButton("start");
51
stop = new JButton("stop");
52
start.addActionListener(this);
53
stop.addActionListener(this);
54
panel.add(start);
55
panel.add(stop);
56
}
57
public Object getCellEditorValue()
{
58
return null;
59
}
60
public Component getTableCellEditorComponent(
61
JTable table, Object value, boolean isSelected, int row, int column)
{
62
this.table = table;
63
this.row = row;
64
return panel;
65
}
66
67
public void actionPerformed(ActionEvent e)
{
68
if(e.getSource() == start)
{
69
states[row] = "start";
70
}else
{
71
states[row] = "stop";
72
}
73
((AbstractTableModel)table.getModel()).fireTableCellUpdated(row, 0);
74
}
75
76
}
77
78
// cell render
79
class Renderer extends JComponent implements TableCellRenderer
{
80
TableCellEditor editor;
81
Renderer(TableCellEditor editor)
{
82
this.editor = editor;
83
}
84
public Component getTableCellRendererComponent(JTable table, Object value,
85
boolean isSelected, boolean hasFocus, int row, int column)
{
86
return editor.getTableCellEditorComponent(table, value, isSelected, row, column);
87
}
88
}
89
90
public TableTest2()
{
91
super("editor used by renderer");
92
table = new JTable(new TableModel());
93
TableColumn tableColumn = table.getColumnModel().getColumn(1);
94
tableColumn.setCellRenderer(new Renderer(new Editor()));
95
tableColumn.setCellEditor(new Editor());
96
97
this.getContentPane().setLayout(new BorderLayout());
98
this.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
99
this.setSize(500, 300);
100
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
101
}
102
public static void main(String[] args)
{
103
new TableTest2().show();
104
}
105
}
