MyButtonEditor类继承DefaultCellEditor


import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JTextField;

public class MyButtonEditor extends DefaultCellEditor
{
	private static final long	serialVersionUID	= -9072499029544974642L;
	protected JButton			btn;
	private String				text;
	private boolean				flag;

	public MyButtonEditor(JTextField textField)
	{
		super(textField);
		btn = new JButton();
		btn.setOpaque(true);
		btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				fireEditingStopped();
			}
		});
	}

	//得到按钮控件
	public Component getTableCellEditorComponent(JTable table, Object object,
			boolean isSelected, int row, int column)
	{
		text = (object == null) ? "" : object.toString();
		btn.setText(text);
		btn.setHorizontalAlignment(JButton.LEFT);
		btn.setHorizontalTextPosition(JButton.LEFT);
		btn.setVerticalTextPosition(JButton.BOTTOM);
		flag = true;
		return btn;
	}

	public Object getCellEditorValue()
	{
		return new String(text);
	}

	public boolean stopCellEditing()
	{
		flag = false;
		return super.stopCellEditing();
	}

	//按钮事件
	protected void fireEditingStopped()
	{
		super.fireEditingStopped();
	}

}

你可能感兴趣的:(java,swing)