I have 4 tables in the database: Branch, Subdivisions, Department, and Section. And these tables are filled with data. Now I can’t do several related Combo Boxs so that when I select a value in the main list, the drop-down values ​​in another will change. Below is the code:

private FormLayout createTabFilesLayout() { FormLayout tabLayout = new FormLayout(); HorizontalLayout hLayout = new HorizontalLayout(); hLayout.setWidth("-1px"); hLayout.setHeight("-1px"); hLayout.setSpacing(true); cBoxBranch = new ComboBox(); cBoxBranch.setInputPrompt("Филиал"); cBoxBranch.setWidth("250px"); cBoxBranch.setHeight("33px"); cBoxBranch.setImmediate(true); cBoxBranch.setNullSelectionAllowed(false); cBoxBranch.setInvalidAllowed(false); Iterator<Branch> bran = branch.iterator(); while(bran.hasNext()) { Branch rt = bran.next(); cBoxBranch.addItem(rt); cBoxBranch.setItemCaption(rt, rt.getBranch_name().trim()); if (applicationForm.getId() != null) { if (rt.getId_branch().equals(applicationForm.getRecords().getId_branch().getId_branch())) { applicationForm.getRecords().setId_branch(rt); } } } hLayout.addComponent(cBoxBranch); binder.bind(cBoxBranch, "id_branch"); tabLayout.addComponent(hLayout); cBoxSubdivision = new ComboBox(); cBoxSubdivision.setInputPrompt("Подразделения"); cBoxSubdivision.setWidth("250px"); cBoxSubdivision.setHeight("33px"); cBoxSubdivision.setImmediate(true); cBoxSubdivision.setNullSelectionAllowed(false); cBoxSubdivision.setInvalidAllowed(false); Iterator<Subdivision> sub = subdivisions.iterator(); while (sub.hasNext()) { Subdivision rt = sub.next(); cBoxSubdivision.addItem(rt); cBoxSubdivision.setItemCaption(rt, rt.getSubdivision().trim());; if (applicationForm.getId() != null) { if(rt.getId_subdivision().equals(applicationForm.getRecords().getId_subdivision().getId_subdivision())) { applicationForm.getRecords().setId_subdivision(rt); } } } hLayout1.addComponent(cBoxSubdivision); binder.bind(cBoxSubdivision, "id_subdivision"); tabLayout.addComponent(hLayout1); cBoxDep = new ComboBox(); cBoxDep.setInputPrompt("Отдел"); cBoxDep.setWidth("300px"); cBoxDep.setHeight("33px"); cBoxDep.setImmediate(true); cBoxDep.setNullSelectionAllowed(false); Iterator<Department> dep = departments.iterator(); while (dep.hasNext()) { Department rt = dep.next(); cBoxDep.addItem(rt); cBoxDep.setItemCaption(rt, rt.getDepartment_name().trim()); if(applicationForm.getId() != null) { if(rt.getId_department().equals(applicationForm.getRecords().getId_department().getId_department())) { applicationForm.getRecords().setId_department(rt); } } } hLayout2.addComponent(cBoxDep); binder.bind(cBoxDep, "id_department"); tabLayout.addComponent(hLayout2); cBoxSec = new ComboBox(); cBoxSec.setInputPrompt("Сектор"); cBoxSec.setWidth("300px"); cBoxSec.setHeight("33px"); cBoxSec.setImmediate(true); cBoxSec.setNullSelectionAllowed(false); Iterator<Section> sec = sections.iterator(); while(sec.hasNext()) { Section rt = sec.next(); cBoxSec.addItem(rt); cBoxSec.setItemCaption(rt, rt.getSection_name().trim()); if(applicationForm.getId()!=null) { if(rt.getId_section().equals(applicationForm.getRecords().getId_section().getId_section())){ applicationForm.getRecords().setId_section(rt); } } } hLayout3.addComponent(cBoxSec); binder.bind(cBoxSec, "id_section"); tabLayout.addComponent(hLayout3); 

In this case, data appear in the drop-down box, but there is no bundle with other ComboBox. Combo Box with data

Next, I created the CreatSubdivisionList() CreatDepartmentList() CreatSectionList() method CreatSubdivisionList() CreatDepartmentList() CreatSectionList()

  private FormLayout createTabFilesLayout() { FormLayout tabLayout = new FormLayout(); cBoxSubdivision = new ComboBox(); cBoxSubdivision.setInputPrompt("Подразделения"); cBoxSubdivision.setWidth("250px"); cBoxSubdivision.setHeight("33px"); cBoxSubdivision.setImmediate(true); cBoxSubdivision.setNullSelectionAllowed(false); cBoxSubdivision.setInvalidAllowed(false); CreatSubdivisionList(); tabLayout.addComponent(cBoxSubdivision); binder.bind(cBoxSubdivision, "id_subdivision"); cBoxSubdivision.addListener(this); private void CreatSubdivisionList() { if (applicationForm.getRecords().getId_branch()!=null) { subdivisions = SBranchService.getSubdivisionById(applicationForm.getRecords().getId_branch().getId_branch(), app.getDbHelper().getConnectionPool()); cBoxSubdivision.removeAllItems(); Iterator<Subdivision> subd = subdivisions.iterator(); while (subd.hasNext()) { Subdivision rt = subd.next(); cBoxSubdivision.addItem(rt); cBoxSubdivision.setItemCaption(rt, rt.getSubdivision().trim()); if(applicationForm.getId() !=null) { if (rt.getId_subdivision().equals(applicationForm.getRecords().getId_subdivision().getId_subdivision())) { applicationForm.getRecords().setId_subdivision(rt); } } } } } 

for others, too, a similar code. In this case, the drop-down box is empty. enter image description here

I need to choose a Branch, and the rest in the drop-down list appeared only those divisions - the department and sector that are tied to this Branch.

Thanks in advance for any information!

    1 answer 1

    It all depends on how the data is related. For example, there is a HashMap , where the key is the value in the main Combox , and the value is the data of the second, then the binding can be done like this:

      Map<String, List<String>> directFilials = new HashMap<>(); directFilials.put("FirstDir", Arrays.asList("Dir1_1", "Dir1_2", "Dir1_3")); directFilials.put("SecondDir", Arrays.asList("Dir2_1", "Dir2_2", "Dir2_3")); directFilials.put("ThirdDir", Arrays.asList("Dir3_1", "Dir3_2", "Dir3_3")); // Put customary greeting in a cell ComboBox<String> dir = new ComboBox("The main CB"); ComboBox<String> fil = new ComboBox("The dependable CB"); dir.setItems(directFilials.keySet()); dir.addSelectionListener(event -> { Optional<String> value = dir.getSelectedItem(); fil.clear(); fil.setItems(directFilials.get(value.get())); });