上文的例子主要的动作在editor上,不过editor只有在点击在cell单元的时候才会显示,所以还不得不造一个界面跟editor一样的 renderer进行“造假”,当然由于这个例子只有两个按钮,所以renderer的代码不多,不过如果editor的组件很多,而且经常需要改动那不 就麻烦了,所以我们可以考虑一下让renderer复用editor的显示部分,以下的代码大家看看就明白:
import java.awt.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.table.*;
public class TableTest2 extends JFrame {
JTable table;
String[] states = new String[]{"stop", "stop", "stop"};
// model
class TableModel extends AbstractTableModel{
public int getColumnCount() {
return 2;
}
public int getRowCount() {
return states.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
if(columnIndex == 0){
return states[rowIndex];
}
return null;
}
public String getColumnName(int columnIndex) {
if(columnIndex == 0){
return "state";
}else{
return "operate";
}
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
if(columnIndex == 0){
return false;
}else{
return true;
}
}
}
// cell editor
class Editor extends AbstractCellEditor implements TableCellEditor, ActionListener {
int row;
JTable table;
JPanel panel;
JButton start;
JButton stop;
Editor(){
panel = new JPanel();
panel.setLayout(new GridLayout(1, 2));
start = new JButton("start");
stop = new JButton("stop");
start.addActionListener(this);
stop.addActionListener(this);
panel.add(start);
panel.add(stop);
}
public Object getCellEditorValue() {
return null;
}
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column) {
this.table = table;
this.row = row;
return panel;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == start){
states[row] = "start";
}else{
states[row] = "stop";
}
((AbstractTableModel)table.getModel()).fireTableCellUpdated(row, 0);
}
}
// cell render
class Renderer extends JComponent implements TableCellRenderer{
TableCellEditor editor;
Renderer(TableCellEditor editor){
this.editor = editor;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
return editor.getTableCellEditorComponent(table, value, isSelected, row, column);
}
}
String[] states = new String[]{"stop", "stop", "stop"};
// model
class TableModel extends AbstractTableModel{
public int getColumnCount() {
return 2;
}
public int getRowCount() {
return states.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
if(columnIndex == 0){
return states[rowIndex];
}
return null;
}
public String getColumnName(int columnIndex) {
if(columnIndex == 0){
return "state";
}else{
return "operate";
}
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
if(columnIndex == 0){
return false;
}else{
return true;
}
}
}
// cell editor
class Editor extends AbstractCellEditor implements TableCellEditor, ActionListener {
int row;
JTable table;
JPanel panel;
JButton start;
JButton stop;
Editor(){
panel = new JPanel();
panel.setLayout(new GridLayout(1, 2));
start = new JButton("start");
stop = new JButton("stop");
start.addActionListener(this);
stop.addActionListener(this);
panel.add(start);
panel.add(stop);
}
public Object getCellEditorValue() {
return null;
}
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column) {
this.table = table;
this.row = row;
return panel;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == start){
states[row] = "start";
}else{
states[row] = "stop";
}
((AbstractTableModel)table.getModel()).fireTableCellUpdated(row, 0);
}
}
// cell render
class Renderer extends JComponent implements TableCellRenderer{
TableCellEditor editor;
Renderer(TableCellEditor editor){
this.editor = editor;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
return editor.getTableCellEditorComponent(table, value, isSelected, row, column);
}
}
public TableTest2(){
super("editor used by renderer");
table = new JTable(new TableModel());
TableColumn tableColumn = table.getColumnModel().getColumn(1);
tableColumn.setCellRenderer(new Renderer(new Editor()));
tableColumn.setCellEditor(new Editor());
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
this.setSize(500, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TableTest2().show();
new TableTest2().show();
}
}