0
我想在每一行的末尾添加按鈕,到目前爲止我已經使用cellRenderer來做這件事。而且我已經成功地在快照中的每一行中顯示按鈕。 但是,當我點擊按鈕沒有行動發生。 我想點擊按鈕時調用一個新框架。 下面是我的源代碼:TableCellEditor類不工作
1.對於填充的JTable
try
{
String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=c:\\ninedots.accdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(url);
String sql1="SELECT RMA_No,CallDate,Source,Item,BookingDesc,Customer,Contact,CallType,CallMode,MaterialRequired,TotalMaterialRequiredCost,AssignedTo,CallStatus FROM RMA_Master WHERE (((CallDate) Between #"+fromdate1+"# And #"+todate1+"#)) AND CallStatus='"+Status+"'";
PreparedStatement pst=con.prepareStatement(sql1);
ResultSet rs=pst.executeQuery();
dtm=(DefaultTableModel) DbUtils.resultSetToTableModel(rs);
dtm.addColumn("Action");
table.setModel(dtm);
table.getColumn("Action").setCellRenderer(new ButtonCellRenderer());
table.getColumn("Action").setCellEditor(new ButtonCellEditor());
rs.close();
pst.close();
con.close();
}
catch(Exception ex)
{
System.err.print("Exception: ");
System.err.println(ex.getMessage());
}
2.ButtonCellRenderer類
public class ButtonCellRenderer extends JButton implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value != null) {
setText("Edit" + value.toString());
} else {
setText("Edit");
}
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(UIManager.getColor("Button.background"));
}
return this;
}
}
3.ButtonCellEditor類
public class ButtonCellEditor extends AbstractCellEditor implements TableCellEditor {
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton editor;
private Object value;
private int row;
private JTable table;
public ButtonCellEditor() {
editor = new JButton();
editor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (table != null) {
fireEditingStopped();
TableModel model = table.getModel();
if (model instanceof DefaultTableModel) {
((DefaultTableModel) model).removeRow(row);
}
}
}
});
}
@Override
public boolean isCellEditable(EventObject e) {
return true;
}
@Override
public Object getCellEditorValue() {
return value;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
this.table = table;
this.row = row;
this.value = value;
if (value != null) {
editor.setText("Delete row " + value.toString());
} else {
editor.setText("Delete Me");
}
if (isSelected) {
editor.setForeground(table.getSelectionForeground());
editor.setBackground(table.getSelectionBackground());
} else {
editor.setForeground(table.getForeground());
editor.setBackground(UIManager.getColor("Button.background"));
}
return editor;
}
}
請推薦的解決方案對於繼Problem.or我在哪裏做錯了?
它你worked..can幫我優化的代碼?因爲現在它變得非常複雜:(我的意思是我現在可以刪除cellEditor類嗎? –