how can i add JButton to last two column cells of row?

旧街凉风 提交于 2019-12-02 09:57:12

You'll need to utilise a custom cell renderer (and I'd guess custom editor to handle the click events).

Checkout Using Custom Renderers and Using Other Editors

You'll probably also want to have a look at CellEditor.isCellEditable to set the number of clicks to activate the button

UPDATE

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

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!