0

我想将 JButton 添加到行的最后两列单元格中..如何做到这一点....请帮助我...

String[] columnNames= {"EmployeeId","Name", "Designation", "JoiningDate", "DOB","ContactNo","MailId","Password","Edit","Remove"};
final DefaultTableModel model1 = new DefaultTableModel(rows, columnNames);
model1.addColumn("columnNames, data");
table3= new JTable(model1);
 table3.getColumn("Edit").setCellEditor(new ButtonEditor(new JCheckBox()));
table3.getColumn("Remove").setCellEditor(new ButtonEditor(new JCheckBox()));
table3.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table3.setRowHeight( 32 );
JScrollPane pane= new JScrollPane(table3);
table3.setPreferredScrollableViewportSize(table3.getPreferredSize());
JTableHeader header = table3.getTableHeader();
header.setBackground(Color.CYAN );
pane.setBackground(Color.BLACK);
employeeFrame.add(pane) ; 
table3.getModel();
4

1 回答 1

2

您需要使用自定义单元格渲染器(我猜自定义编辑器来处理点击事件)。

使用自定义渲染器使用其他编辑器结帐

您可能还想查看CellEditor.isCellEditable来设置激活按钮的点击次数

更新

public class MyTatbleEditor {

    public static void main(String[] args) {
        new MyTatbleEditor();
    }

    public MyTatbleEditor() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {

                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setLayout(new BorderLayout());

                Vector rows = new Vector();
                for (int index = 0; index < 10; index++) {
                    Vector data = new Vector();
                    data.add(new Object());
                    rows.add(data);
                }
                Vector cols = new Vector();
                cols.add("One");
                DefaultTableModel model = new DefaultTableModel(rows, cols);

                JTable table = new JTable(model);

                table.getColumn("One").setCellEditor(new MyTableButtonEditor());
                table.getColumn("One").setCellRenderer(new MyTableButtonRenderer());

                frame.add(new JScrollPane(table));

                frame.setVisible(true);


            }
        });
    }

    protected class MyTableButtonEditor extends AbstractCellEditor implements TableCellEditor {

        private JButton button;
        private int column;
        private int row;

        public MyTableButtonEditor() {
            button = new JButton("Click me if you dare");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            JOptionPane.showMessageDialog(button, "How dare you :(", "Fancy that", JOptionPane.INFORMATION_MESSAGE);
                        }
                    });
                }
            });
        }

        @Override
        public boolean isCellEditable(EventObject e) {
            boolean isEditable = false;
            if (e instanceof MouseEvent) {
                MouseEvent me = (MouseEvent) e;
                if (me.getButton() == MouseEvent.BUTTON1 && me.getID() == MouseEvent.MOUSE_PRESSED) {
                    isEditable = true;
                }
            } else if (e instanceof KeyEvent) {
                KeyEvent ke = (KeyEvent) e;
                if (ke.getID() == KeyEvent.KEY_PRESSED && (ke.getKeyCode() == KeyEvent.VK_SPACE || ke.getKeyCode() == KeyEvent.VK_ENTER)) {
                    isEditable = true;
                }
            }
            return isEditable;
        }

        @Override
        public Object getCellEditorValue() {
            return new Object();
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            if (isSelected) {
                button.requestFocus();
            }
            this.column = column;
            this.row = row;
            return button;
        }
    }

    protected class MyTableButtonRenderer implements TableCellRenderer {

        private JButton button;

        public MyTableButtonRenderer() {
            button = new JButton("Click me if you dare");
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            return button;
        }

    }
}
于 2012-10-03T10:32:36.763 回答