如何将JButton添加到行的最后两个列单元格? [英] how can i add JButton to last two column cells of row?

查看:134
本文介绍了如何将JButton添加到行的最后两个列单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

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

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

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

Checkout Using Custom Renderers and Using Other Editors

您可能还想查看 CellEditor.isCellEditable 设置数量点击以激活按钮

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

更新

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

    }
}

这篇关于如何将JButton添加到行的最后两个列单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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