我正在尝试在每行的末尾添加按钮,到目前为止,我已经使用cellRenderer做到了这一点,而且我已经成功地在每行中显示了按钮,如快照中一样。

但是,当我单击按钮时,没有任何动作发生。
我想在单击按钮时调用一个新框架。
以下是我的源代码:

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;
    }


}


请提出以下问题的解决方案。或者我在哪里做错了?

最佳答案

检出Table Button Column以获取自定义渲染器/编辑器,该渲染器/编辑器允许您将带有自定义Action的按钮添加到任何列。

您编写的唯一代码是Action,因此易于实现:

Action delete = new AbstractAction()
{
    public void actionPerformed(ActionEvent e)
    {
        JTable table = (JTable)e.getSource();
        int modelRow = Integer.valueOf( e.getActionCommand() );
        ((DefaultTableModel)table.getModel()).removeRow(modelRow);
    }
};

关于java - TableCellEditor类无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29647762/

10-08 22:03