需要澄清如何为以下代码添加动作侦听器

时间:2012-12-13 04:55:58

标签: java swing awt actionlistener

  

可能重复:
  How to add button in a row of JTable in Swing java

如何添加动作侦听器以删除以下程序的相应行。我无法做到这一点...请给我澄清... 对于渲染器和编辑器中的按钮,我需要添加actionlistener ..

public class TableSample7 extends DefaultTableModel {
private static final long serialVersionUID = 1L;
static int FRAME_WIDTH=800;
static int FRAME_HEIGHT=400;
int i=0;
JFrame frame=new JFrame("Sample");
Object data[][]={{"","","","Delete"}};
Object col[]={"Fname","Lname","Gender","Button"};
public DefaultTableModel model = new DefaultTableModel(data,col);
JTable table=new JTable(model);
JButton addButton=new JButton("Add");
public TableSample7(){
text(table,table.getColumnModel().getColumn(0));
text(table,table.getColumnModel().getColumn(1));
text(table,table.getColumnModel().getColumn(2));
table.getColumn("Button").setCellRenderer(new ButtonRenderer1());
table.getColumn("Button").setCellEditor(new ButtonEditor1(new JCheckBox()));
JTableHeader header=table.getTableHeader();
header.setLayout(new FlowLayout(FlowLayout.TRAILING,70,-5));
header.add(addButton);
addButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
i=i+1;
model.insertRow(i,new Object[]{"","","","Delete"});
}
});
header.setBounds(0, 0, 50, 25);
table.setRowHeight(100);
frame.setLayout(new BorderLayout());
frame.add(table.getTableHeader(),BorderLayout.PAGE_START);
frame.add(table,BorderLayout.CENTER);
frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void text(JTable table,TableColumn column){
JTextField txt=new JTextField();
txt.setSize(80,40);
column.setCellEditor(new DefaultCellEditor(txt));
DefaultTableCellRenderer renderer=new DefaultTableCellRenderer();
column.setCellRenderer(renderer);
}
public void check(JTable table,TableColumn column){
JCheckBox c1=new JCheckBox();
JCheckBox c2=new JCheckBox();
column.setCellEditor(new DefaultCellEditor(c1));
column.setCellEditor(new DefaultCellEditor(c2));
DefaultTableCellRenderer renderer=new DefaultTableCellRenderer ();
column.setCellRenderer(renderer);
}
public static void main(String args[]){
new TableSample7();
}
}
class ButtonRenderer1 extends JButton implements TableCellRenderer {
private static final long serialVersionUID = 1L;
public ButtonRenderer1() {
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
setText((value == null) ? "" : value.toString());
return this;
}
}
class ButtonEditor1 extends DefaultCellEditor {
private static final long serialVersionUID = 1L;
protected JButton button;
private String label;
private boolean isPushed;
public ButtonEditor1(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JTable table = (JTable) SwingUtilities.getAncestorOfClass(
JTable.class, (Component) e.getSource());
if (table != null) {
int row = table.getEditingRow();
((DefaultTableModel)table.getModel()).removeRow(row);    
table.getCellEditor().stopCellEditing();              
fireEditingStopped();
}
}});
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (isSelected) {
button.setForeground(table.getSelectionForeground());
button.setBackground(table.getSelectionBackground());
} else {
button.setForeground(table.getForeground());
button.setBackground(table.getBackground());
}
label = (value == null) ? "" : value.toString();
button.setText(label);
isPushed = true;
return button;
}
@Override
public Object getCellEditorValue() {
if (isPushed) {
JOptionPane.showMessageDialog(button, label + ": Ouch!");
System.out.print(label);
// button.addActionListener(new ActionListener(){
}
isPushed = false;
return label;
}
@Override
 public boolean stopCellEditing() {
 isPushed = false;
 return super.stopCellEditing();
  }
 @Override
 protected void fireEditingStopped() {
 super.fireEditingStopped();
  }
  }

0 个答案:

没有答案