TableCellRenderer和TableCellEditor(一)
转载:http://blog.sina.com.cn/s/blog_661a3fce0100mske.html
最近不少朋友问我TableCellRenderer和TableCellEditor的用法,趁今天周末有空写了几个麻雀虽小五脏俱全的例子供大家参考。
简单来说:
简单来说:
TableCellRenderer就是用来绘制展示当前cell单元数值内容的,你可以用文字、数值或者图片来表示内容,当然最强大展示方式的就是通过自定义自己的renderer组件,通过Java2D来绘制各种天花乱坠的效果。
TableCellEditor主要是用来当用户点击在具体cell时进行编辑的组件,所以
TableCellEditor除了具有TableCellRenderer一样的绘制功能外还可以进行交互动作,例如在cell上出现下拉框、勾选框甚
至通过按钮弹出更复杂的对话框让用户进行输入编辑。
以下是在cell中嵌入start、stop两个按钮的表格,注意这里的renderer只是用来显示效果,并没有进行交互动作,真正进行动作的是当你将鼠标点击在cell上时,JTable定位上去的editor,所以不要徒劳在renderer上做点击事项处理,renderer是不会接受到交互事项的,renderer只是个画笔,你看到的start、stop按钮只是这个画笔画出来的内容,是虚的并没有真正的组件在上面。
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 TableTest1 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{
JPanel panel;
JButton start;
JButton stop;
Renderer(){
panel = new JPanel();
panel.setLayout(new GridLayout(1, 2));
start = new JButton("start");
stop = new JButton("stop");
panel.add(start);
panel.add(stop);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
return panel;
}
}
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{
JPanel panel;
JButton start;
JButton stop;
Renderer(){
panel = new JPanel();
panel.setLayout(new GridLayout(1, 2));
start = new JButton("start");
stop = new JButton("stop");
panel.add(start);
panel.add(stop);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
return panel;
}
}
public TableTest1(){
super("renderer and editor self-existent");
table = new JTable(new TableModel());
TableColumn tableColumn = table.getColumnModel().getColumn(1);
tableColumn.setCellRenderer(new Renderer());
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 TableTest1().show();
new TableTest1().show();
}
}