Search code examples
javaswingjtablejscrollpanejbutton

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


i want to add JButton to last two column cells of row..How can do that....pls help me...

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();

Solution

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