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();
}
}
MyButtonEditor类继承DefaultCellEditor
最新推荐文章于 2019-07-12 21:33:39 发布