jButton无法在jTable中单击 [英] jButton not clickable in jTable

查看:117
本文介绍了jButton无法在jTable中单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题,我无法单击按钮.它们的行为就像只是带有按钮设计的文本字段.

I got the problem, that I cannot click on buttons. They behave like they are just textfields with the design of buttons.

我的Main:

    tableModStudents = (DefaultTableModel) studentsTable.getModel();
    studentsTable.getColumn(studentsTable.getColumnName(8))
                 .setCellRenderer(new JButtonRenderer());
    studentsTable.getColumn(studentsTable.getColumnName(8))
                 .setCellEditor(new JButtonEditor());

我的CellRenderer:

public class JButtonRenderer implements TableCellRenderer {    
    private JButton button = new JButton();

    public Component getTableCellRendererComponent(JTable table,
            Object buttonText, boolean isSelected, boolean hasFocus, 
            int row, int column) {
        table.setShowGrid(true);
        button.setText("Details");
        button.setToolTipText(buttonText.toString());
        return button;
    }
}

我的CellEditor:

    public class JButtonEditor extends AbstractCellEditor implements TableCellEditor {

    private JButton button;
    private String txt;

    public JButtonEditor() {
        super();
        button = new JButton();
        button.setOpaque(true);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                System.out.println("Button gedrückt!");
            }
        });
    }

    public Object getCellEditorValue() {
        return null;
    }

    public boolean isCellEditable(EventObject anEvent) {
        return true;
    }

    public boolean shouldSelectCell(EventObject anEvent) {
        return false;
    }

    public boolean stopCellEditing() {
        return super.stopCellEditing();
    }

    public void cancelCellEditing() {
    }

    public void addCellEditorListener(CellEditorListener l) {
    }

    public void removeCellEditorListener(CellEditorListener l) {
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        txt = (value == null) ? "" : value.toString();
        button.setText(txt);
        return button;
    }
}

您能找到与此有关的问题吗?这让我发疯……

Can you find the issue with that? It drives my crazy...

非常感谢:)

推荐答案

查看它将按钮渲染器和编辑器组合在一个类中.

It combines a button renderer and editor in a single class.

您需要做的就是提供自定义的Action,当您调用按钮时(通过单击它或通过调用它的助记符)将被调用.

All you need to do is provide the custom Action to be invoked when you invoke the button (either by clicking on it or by invoking its mnemonic).

这篇关于jButton无法在jTable中单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆