如何在JTable单元格中放置可编辑的JTextField?

时间:2016-09-01 19:13:18

标签: java jtable jlabel jtextfield jcheckbox

我有一个以下类,我在JLabel和JTextField中设置值。

        class TextBoxCellRenderer extends JPanel implements TableCellRenderer {
            JCheckBox checkBox;

            public Component getTableCellRendererComponent(
                    final JTable table, Object value,
                    boolean isSelected, boolean hasFocus,
                    int row, int column) {
                switch (row){
                    case 0://UserID, UserIDType
                        switch (column) {
                            case 0:
                                this.add(new JLabel(value.toString(), JLabel.LEFT));
                                break;
                            case 1:
                                this.add( new JTextField( value.toString(), 15  ) );
                                break;
                            case 2:
                                this.add(new JLabel(value.toString(), JLabel.LEFT));
                                break;
                            case 3:
                                this.add( new JTextField( value.toString(), 15  ) );
                                break;

                        }
                        break;
                    case 1: //GivenName, FamilyName
                        switch (column) {
                            case 0:
                                this.add(new JLabel(value.toString(), JLabel.LEFT));
                                break;
                            case 1:
                                this.add( new JTextField( value.toString(), 15  ) );
                                break;
                            case 2:
                                this.add(new JLabel(value.toString(), JLabel.LEFT));
                                break;
                            case 3:
                                this.add( new JTextField( value.toString(), 15  ) );
                                break;
                        }
                    break;
//More cases here
    case 10: //DateOfBirth, Sex
                    switch (column) {
                        case 0:
                            this.add(new JLabel(value.toString(), JLabel.LEFT));
                            break;
                        case 1:
                            checkBox = new JCheckBox();
                            checkBox.setToolTipText("Set 0 OR 1");
                            checkBox.setEnabled(true);
                            if(value.toString().equals("1"))
                                checkBox.setSelected(true);
                            if(value.toString().equals("0"))
                                checkBox.setSelected(false);
                            this.add(checkBox);
                            break;
                        case 2:
                            this.add(new JLabel(value.toString(), JLabel.LEFT));
                            break;
                        case 3:
                            this.add(new JLabel(value.toString(), JLabel.LEFT));
                            break;
                    }
                    break;
               }


                return this;
            }
        }

从调用方法我得到以下代码:

private void renderAdminInfoTable() {
        adminInfoTable = new JTable(){public TableCellRenderer getCellRenderer( int row, int column ) {
            return new TextBoxCellRenderer();
        }};


adminInfoTable.setPreferredScrollableViewportSize(adminInfoTable.getPreferredSize());
    adminInfoTable.setFillsViewportHeight(true);
    adminInfoTable.setBackground(ARSColour.TRANSPARENTBLUE);
    adminInfoTable.setGridColor(new Color(128, 128, 128, 50));
    adminInfoTable.setRowHeight(32);
    adminInfoTable.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    adminInfoTable.setShowVerticalLines(false);

    DefaultTableModel model = new DefaultTableModel() {
        @Override
        public boolean isCellEditable(int row, int column) {
            switch (row){
                case 0: //UserID, UserIDType
                    switch (column) {
                        case 0:
                            return false;
                        case 1:
                            return true;
                        case 2:
                            return false;
                        case 3:
                            return true;
                        default:
                            return false;
                    }

                case 1: //GivenName, FamilyName
                    switch (column) {
                        case 0:
                            return false;
                        case 1:
                            return true;
                        case 2:
                            return false;
                        case 3:
                            return true;
                        default:
                            return false;
                    }
//More cases here   
case 10: //Active
                    switch (column) {
                        case 0:
                            return false;
                        case 1:
                            return true;
                        case 2:
                            return false;
                        case 3:
                            return false;
                        default:
                            return false;
                    }
                default:
                    return false;
            }
        }
    };

    adminInfoTable.setModel(model); 

我的问题是:我得到的所有JTextField都不可编辑。当我双击JTextFields时,可编辑表格单元格变为活动状态,我可以在那里编辑并传递已编辑的信息以保存到数据库中。

我的问题是:如何使这些JTextFields和JCheckbox可编辑并从中检索所有已编辑的数据?

问候

0 个答案:

没有答案