0

I'm working on showing data from a database on a Grid and works fine so far. I wanted to add editing capabilities and it did work but I'm struggling to make it work only for some columns and not others.

Here's the current code:

SettingJpaController sc
                = new SettingJpaController(Lookup.getDefault()
                        .lookup(DatabaseManager.class)
                        .getEntityManagerFactory());
        Grid<Setting> settings = new Grid<>();
        TextField value = new TextField();
        settings.getEditor().setEnabled(true);
        settings.setItems(sc.findSettingEntities());
        settings.addColumn(Setting::getSettingName)
                .setCaption("Name");
        settings.addColumn(setting -> setting.getSettingValue())
                .setCaption("Value")
                .setEditorComponent(value, Setting::setSettingValue)
                .setExpandRatio(1);
        settings.getEditor().addSaveListener((EditorSaveEvent<Setting> event) -> {
            try {
                sc.edit(event.getBean());
            } catch (Exception ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        });
        settings.addColumn(Setting::getDescription).setCaption("Description");

As shown above the editor shows for all fields. For this case I only want the value field to be editable. I tried adding .setEditable(false) to the other columns but I get this error:

java.lang.NullPointerException: Column has no editor binding or component defined

makes no sense to me to have to assign and editor, etc. just to disable it so I'm sure I'm missing something. Any idea?

Note: I'm aware of this question but none of those solutions work for me. Although this answer works, the behavior is odd and undesirable.

javydreamercsw
  • 5,363
  • 13
  • 61
  • 106
  • 1
    Please try adding saveListener to particular column instead of grid. [This](https://demo.vaadin.com/sampler/#ui/grids-and-trees/grid/item-editor) sample might help you. – Dawid Fieluba Dec 07 '17 at 17:50
  • How would I do that? There's no such method on the column. Also the example doen't show me anything I don't have in the code already. – javydreamercsw Dec 07 '17 at 20:45
  • You re right, sorry, Column doesn't have such method. Well your code and sample I posted has 2 differences: `settings.getEditor().setEnabled(true);` at the end and there is no `addSaveListener()` - which is most probably the problem here. Please check it – Dawid Fieluba Dec 07 '17 at 21:24
  • The save listener I needed to persist the changes to the database. Otherwise it gets lost. If I don't enable the editor it just doesn't show up. – javydreamercsw Dec 08 '17 at 16:12
  • I mean setting editor to enabled at the end like in sample, but it's very tiny possibility that its about it. Please check if it works as it should without save listener. I know that you need it but we need to know where the problem actually is – Dawid Fieluba Dec 08 '17 at 16:44
  • I tried it and made no difference. – javydreamercsw Dec 11 '17 at 21:52

0 Answers0