Exception occured while dynamically changing cell values if overriding createCellConstraint(Object value, int row, int column) method of Table

人走茶凉 提交于 2020-01-06 15:13:00

问题


I have created the Table component by overriding two methods - createCell(Object value, final int row, final int column, boolean editable) method and createCellConstraint(Object value, int row, int column). Below is the whole code I used for Table.

String[][] tableData = {
                {"Density", "Volume Flow", "T (in)","T (out)", "Specific Heat", "Density", "Volume Flow", 
                    "T (in)","T (out)", "Specific Heat", "Duty", "UA"},
                {"1","2", "3","4","5", "6", "7", "8", "9","10","11", "12", "13", "14"}
        };

    final CustomTableModel tableModel = new CustomTableModel (
            new String[]  {"Hot Side", "","","","","Cold Side","","","","","",""}, tableData,true);

    final String[] r =   {"1","2", "3","4","5", "6", "7", "8", "9","10","11", "12"};

    dataTable =  new Table(tableModel) {

        @Override
        protected Constraint createCellConstraint(Object value, int row, int column) {

            Constraint con = super.createCellConstraint(value, row, column);
            if(row == -1 && (column == 0 || column == 5) ) {
                con.setHorizontalSpan(5);
            }
            return con;
        }

        @Override
        protected Component createCell(Object value, final int row, final int column, boolean editable) {

            if(row == -1) {

                final Button headerButton = new Button((String)value);
                headerButton.setUIID(getUIID() + "Header");
                headerButton.getUnselectedStyle().setAlignment(Component.CENTER);
                headerButton.getSelectedStyle().setAlignment(Component.CENTER);
                headerButton.setFlatten(true);
                return headerButton;
            }

            if(row == 0) {

                final Button cell = new Button((String)value);
                cell.setUIID(getUIID() + "Cell");
                cell.getUnselectedStyle().setAlignment(Component.CENTER);
                cell.getSelectedStyle().setAlignment(Component.CENTER);

                cell.setFlatten(true);
                cell.addActionListener(new ActionListener() {
                    final InteractionDialog dlg = new InteractionDialog("Hello"); 
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        cell.setEnabled(false);
                        dlg.setLayout(new BorderLayout()); 
                        dlg.addComponent(BorderLayout.CENTER, new Label(cell.getText())); 
                        Button close = new Button("Close"); 
                        close.addActionListener(new ActionListener() { 
                            public void actionPerformed(ActionEvent evt) { 
                                cell.setEnabled(true);
                                dlg.dispose(); 
                            } 
                        }); 

                        dlg.addComponent(BorderLayout.SOUTH, close); 
                        dlg.show(0, DISPLAY_HEIGHT/2, DISPLAY_WIDTH/2, 0); 
                    }
                });

                return cell;
            }

            if(row >= 1) {

                if(column == 2 || column == 3 || column == 6 || column == 7) {
                    final Button cell = new Button((String)value);
                    cell.setUIID(getUIID() + "Cell");
                    cell.getUnselectedStyle().setAlignment(Component.CENTER);
                    cell.getSelectedStyle().setAlignment(Component.CENTER);

                    cell.setFlatten(true);
                    cell.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent evt) {

                            Container body  = new Container();
                            body.setLayout(new GridBagLayout());

                            Label value = new Label("Value");
                            value.getStyle().setBgTransparency(0);

                            TextField text = new TextField(TextArea.DECIMAL);
                            text.getStyle().setBgTransparency(0);
                            text.setText(cell.getText());

                            Label unit = new Label("Unit");
                            unit.getStyle().setBgTransparency(0);

                            ComboBox<String> uom = new ComboBox<String>();
                            List<String> uomList = DbServices.getAllConversionGroupVars(UnitConstants.TEMPERATURE);
                            for(String u : uomList) {
                                uom.addItem(u);
                            }
                            uom.getStyle().setBgTransparency(0);
                            uom.setCommandList(true);

                            body.addComponent(new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), value);
                            body.addComponent(new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), text);
                            body.addComponent(new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), unit);
                            body.addComponent(new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), uom);

                            Command cmd = Dialog.show("Add Values", body, new Command[] {new Command("OK"), new Command("Cancel")});

                            if(cmd.getCommandName().equals("OK")) {
                                String from_var = uom.getSelectedItem();
                                double val = Double.parseDouble(cell.getText());
                                String convertedValue = DbServices.getConversion(from_var, UnitConstants.KELVIN, val);
                                System.out.println(convertedValue+":"+ from_var);
                                tableModel.setValueAt(row, column, convertedValue);
                            }

                            if(cmd.getCommandName().equals("Cancel")) {
                                return;
                            }
                            int totalRow = tableModel.getRowCount();
                            if(!(totalRow > row +1)) {
                                tableModel.addRow(r, row);
                            }
                        }
                    });
                    return cell;
                }

                if(column == 0 || column == 1 || column == 4 || column == 5 || column  > 8 ) {

                    final Button cell = new Button((String)value);
                    cell.setUIID(getUIID() + "Cell");
                    cell.getUnselectedStyle().setAlignment(Component.CENTER);
                    cell.getSelectedStyle().setAlignment(Component.CENTER);

                    cell.setFlatten(true);
                    cell.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent evt) {

                            Container body  = new Container();
                            body.setLayout(new GridBagLayout());

                            Label value = new Label("Value");
                            value.getStyle().setBgTransparency(0);

                            TextField text = new TextField();
                            text.getStyle().setBgTransparency(0);
                            text.setText(cell.getText());
                            Label unit = new Label("Unit");
                            unit.getStyle().setBgTransparency(0);

                            ComboBox<String> uom = new ComboBox<String>();

                            List<String> uomList = DbServices.getAllConversionGroupVars(UnitConstants.TEMPERATURE);
                            for(String u : uomList) {
                                uom.addItem(u);
                            }

                            uom.getStyle().setBgTransparency(0);
                            body.addComponent(new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), value);
                            body.addComponent(new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), text);
                            body.addComponent(new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), unit);
                            body.addComponent(new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0), uom);

                            Command cmd = Dialog.show("Add Values", body, new Command[] {new Command("OK"), new Command("Cancel")});

                            if(cmd.getCommandName().equals("OK")) {
                                tableModel.setValueAt(row, column, text.getText());
                            }

                            if(cmd.getCommandName().equals("Cancel")) {
                                return;
                            }

                            int totalRow = tableModel.getRowCount();
                            if(!(totalRow > row +1)) {
                                tableModel.addRow(r, row);
                            }
                        }
                    });
                    return cell;
                }
            } 

            Button cell = new Button("" + value);
            cell.setUIID(getUIID() + "Cell");
            cell.getUnselectedStyle().setAlignment(Component.CENTER);
            cell.getSelectedStyle().setAlignment(Component.CENTER);
            cell.setFlatten(true);
            return cell;
        }
    };

When I'm trying to change cell value for row > 1 its giving below exception. This exception is coming because of createCellConstraint(Object value, int row, int column) method for sure. If I comment this method then only I'm able to change cell values otherwise giving below exception --

java.lang.ArrayIndexOutOfBoundsException: 36
    at com.codename1.ui.table.TableLayout.getCellHorizontalSpan(TableLayout.java:697)
    at com.codename1.ui.table.Table.paintGlass(Table.java:286)
    at com.codename1.ui.Container.paintGlassImpl(Container.java:1004)
    at com.codename1.ui.Component.paintGlassImpl(Component.java:1468)
    at com.codename1.ui.Component.paintComponent(Component.java:1453)
    at com.codename1.ui.Component.paintComponent(Component.java:1401)
    at com.codename1.impl.CodenameOneImplementation.paintDirty(CodenameOneImplementation.java:475)
    at com.codename1.impl.javase.JavaSEPort.paintDirty(JavaSEPort.java:1116)
    at com.codename1.ui.Display.edtLoopImpl(Display.java:1056)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:983)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
java.lang.ArrayIndexOutOfBoundsException: 36
    at com.codename1.ui.table.TableLayout.getCellHorizontalSpan(TableLayout.java:697)
    at com.codename1.ui.table.Table.paintGlass(Table.java:286)
    at com.codename1.ui.Container.paint(Container.java:986)
    at com.codename1.ui.Component.internalPaintImpl(Component.java:1255)
    at com.codename1.ui.Component.paintInternalImpl(Component.java:1229)
    at com.codename1.ui.Component.paintInternal(Component.java:1204)
    at com.codename1.ui.Container.paint(Container.java:975)
    at com.codename1.ui.Component.internalPaintImpl(Component.java:1255)
    at com.codename1.ui.Component.paintInternalImpl(Component.java:1229)
    at com.codename1.ui.Component.paintInternal(Component.java:1204)
    at com.codename1.ui.Container.paint(Container.java:975)
    at com.codename1.ui.Form.paint(Form.java:2808)
    at com.codename1.ui.Component$BGPainter.paint(Component.java:4632)
    at com.codename1.ui.Component.paintBackgroundImpl(Component.java:1616)
    at com.codename1.ui.Component.paintComponentBackground(Component.java:1576)
    at com.codename1.ui.Container.paintComponentBackground(Container.java:1785)
    at com.codename1.ui.Component.internalPaintImpl(Component.java:1240)
    at com.codename1.ui.Form.internalPaintImpl(Form.java:2818)
    at com.codename1.ui.Component.paintInternalImpl(Component.java:1229)
    at com.codename1.ui.Component.paintInternal(Component.java:1204)
    at com.codename1.ui.Component.paintInternal(Component.java:1172)
    at com.codename1.ui.Component.paintComponent(Component.java:1450)
    at com.codename1.ui.Component.paintComponent(Component.java:1401)
    at com.codename1.impl.CodenameOneImplementation.paintDirty(CodenameOneImplementation.java:475)
    at com.codename1.impl.javase.JavaSEPort.paintDirty(JavaSEPort.java:1116)
    at com.codename1.ui.Display.edtLoopImpl(Display.java:1056)
    at com.codename1.ui.Display.invokeAndBlock(Display.java:1187)
    at com.codename1.ui.Display.invokeAndBlock(Display.java:1222)
    at com.codename1.ui.Form.showModal(Form.java:1608)
    at com.codename1.ui.Dialog.showModal(Dialog.java:1089)
    at com.codename1.ui.Dialog.show(Dialog.java:470)
    at com.codename1.ui.Dialog.showPackedImpl(Dialog.java:1386)
    at com.codename1.ui.Dialog.showPacked(Dialog.java:1294)
    at com.codename1.ui.Dialog.showImpl(Dialog.java:1049)
    at com.codename1.ui.Dialog.show(Dialog.java:1031)
    at com.codename1.ui.Dialog.show(Dialog.java:989)
    at com.codename1.ui.Dialog.show(Dialog.java:778)
    at com.codename1.ui.Dialog.show(Dialog.java:731)
    at com.codename1.ui.Dialog.show(Dialog.java:696)
    at com.codename1.ui.Dialog.show(Dialog.java:540)
    at com.codename1.ui.Dialog.show(Dialog.java:792)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:996)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
[EDT] 0:0:0,0 - java.lang.ArrayIndexOutOfBoundsException: 36
[EDT] 0:0:0,1 - java.lang.ArrayIndexOutOfBoundsException: 36

来源:https://stackoverflow.com/questions/29234292/exception-occured-while-dynamically-changing-cell-values-if-overriding-createcel

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