You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TreeItem多级子项配置与设备ID结构化展示技术问询

Solution: Grouping Device IDs in JavaFX TreeItem Structure

Got it, let's build that hierarchical TreeItem structure you're looking for. The key here is first organizing your data into groups so we don't create duplicate parent/equipment nodes, then constructing the TreeItems from that grouped data.

Step 1: Group Your Data

First, we need to organize your ObservableList<Customer_EquipTree> into a nested structure that groups device IDs by their equipment name, which in turn are grouped by their parent node (like "Inlet P1"). This avoids creating duplicate TreeItems for the same parent or equipment.

Assuming your Customer_EquipTree class has getter methods for the parent node (e.g., getParentNode()), equipment name (getEquipmentName()), and device ID (getDeviceId()), here's how to group the data:

// Import necessary classes: java.util.*, java.util.stream.*
Map<String, Map<String, List<Integer>>> groupedData = new HashMap<>();

for (Customer_EquipTree equip : equiptrees) {
    // Get or create the map for the parent node (e.g., "Inlet P1")
    Map<String, List<Integer>> equipmentMap = groupedData.computeIfAbsent(
        equip.getParentNode(), 
        k -> new HashMap<>()
    );
    
    // Get or create the list of IDs for the equipment (e.g., "M&C-SP2500")
    List<Integer> deviceIdList = equipmentMap.computeIfAbsent(
        equip.getEquipmentName(), 
        k -> new ArrayList<>()
    );
    
    deviceIdList.add(equip.getDeviceId());
}

Step 2: Build the TreeItem Hierarchy

Now that we have grouped data, we can construct the TreeItems. We'll create the root node first, then iterate through the grouped data to add parent nodes and their equipment children.

// Root node
TreeItem<String> rootItem = new TreeItem<>("Site Equipment");
rootItem.setExpanded(true);

// Iterate over each parent node (e.g., "Inlet P1", "Inlet P2")
for (Map.Entry<String, Map<String, List<Integer>>> parentEntry : groupedData.entrySet()) {
    String parentNodeName = parentEntry.getKey();
    TreeItem<String> parentItem = new TreeItem<>(parentNodeName);
    
    // Iterate over each equipment under this parent
    for (Map.Entry<String, List<Integer>> equipEntry : parentEntry.getValue().entrySet()) {
        String equipmentName = equipEntry.getKey();
        List<Integer> deviceIds = equipEntry.getValue();
        
        // Format the equipment node text to show all IDs
        // Optional: Sort the IDs if needed with Collections.sort(deviceIds);
        String equipNodeText = String.format(
            "%s → %s",
            equipmentName,
            String.join(", ", deviceIds.stream().map(String::valueOf).toArray(String[]::new))
        );
        
        TreeItem<String> equipItem = new TreeItem<>(equipNodeText);
        parentItem.getChildren().add(equipItem);
    }
    
    rootItem.getChildren().add(parentItem);
}

Key Notes

  • Adjust Getter Methods: If your Customer_EquipTree uses different method names (e.g., getInletName() instead of getParentNode()), update the code to match your actual class structure.
  • Sorting IDs: If you want the device IDs to appear in order, add Collections.sort(deviceIds); right after retrieving the list from the map.
  • Flexibility: If later you want each device ID to be a separate child node under the equipment, you can modify the inner loop to create a TreeItem for each ID instead of joining them into a single string.

内容的提问来源于stack exchange,提问作者Steve Scan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:16:46