JTree,很多条目

时间:2012-01-01 19:07:56

标签: java swing user-interface jtree

所以我习惯通过将JTree添加到其模型中来构建DefaultMutableTreeNode,但是现在我有很多属于1 JTree的条目有更短​​的条目这样做的方法可能就像你做JList,你只需指定一个String table[] = {};

"<html> <b>Shield Hardners</b> </html>", 
"    Ballistic Deflection Array",
"    Explosion Dampening Array",
"    Heat Dissipation Array",
"    Photon Scattering Array",
"<html> <b>Hangar Arrays</b> </html>",
"    Corporate Hangar Array",
"<html> <b>Ship Maintnance Bays</b> </html>",
"    Capital Ship Maintenance Array",
"    Ship Maintenance Array",
"<html> <b>Silos</b> </html>",
"    Biochemical Silo",
"    Catalyst Silo",
"    Coupling Array",
"    General Storage", 
"    Hazardous Chemical Silo", 
"    Silo", 
"<html> <b>Assembly Arrays</b> </html>",
"    Advanced Large Ship Assembly Array",
"    Advanced Medium Ship Assembly Array",
"    Advanced Small Ship Assembly Array",
"    Ammunition Assembly Array",
"    Capital Ship Assembly Array",
"    Component Assembly Array",
"    Drone Assembly Array",
"    Efficient Equipment Assembly Array",
"    Large Ship Assembly Array",
"    Medium Ship Assembly Array",
"    Rapid Equipment Assembly Array",
"    Small Ship Assembly Array",
"<html> <b>Mobile Laboratory</b> </html>",
"    Advanced Mobile Laboratory",
"    Drug Lab",
"    Mobile Laboratory",
"<html> <b>Moon Harvester</b> </html>", 
"    Moon Harvesting Array",
"    Moon Harvesting Array II",
"<html> <b>Reactor Arrays</b> </html>", 
"    Biochemical Reactor Array",
"    Complex Reactor Array", 
"    Medium Biochemical Reactor Array",
"    Simple Reactor Array",
"<html> <b>Refining Arrays</b> </html>", 
"    Intensive Refining Array",
"    Medium Intensive Refining Array", 
"    Refining Array",
"<html> <b>Sov Modules</b> </html>", 
"    Cynosural Generator Array",
"    Cynosural System Jammer", 
"    Jump Bridge", 
"    System Scanning Array",
"<html> <b>Electronic Warfare</b> </html>", 
"    Dread Guristas Ion Field Projection Battery",
"    Guristas Ion Field Projection Battery",
"    Ion Field Projection Battery",
"    Dread Guristas Phase Inversion Battery",
"    Guristas Phase Inversion Battery",
"    Phase Inversion Battery",
"    Dread Guristas Spatial Destabilization Battery",
"    Guristas Spatial Destabilization Battery",
"    Spatial Destabilization Battery",
"    Dread Guristas White Noise Generation Battery",
"    Guristas White Noise Generation Battery",
"    White Noise Generation Battery",
"    Sensor Dampening Battery",
"    Serpentis Sensor Dampening Battery",
"    Shadow Serpentis Sensor Dampening Battery",
"    Stealth Emitter Array",
"    Serpentis Warp Disruption Battery",
"    Shadow Warp Disruption Battery",
"    Warp Disruption Battery",
"    Serpentis Warp Scrambling Battery",
"    Shadow Warp Scrambling Battery",
"    Warp Scrambling Battery",
"    Angel Stasis Webification Battery",
"    Domination Stasis Webification Battery",
"    Stasis Webification Battery",
"<html> <b>Energy Neutralizers</b> </html>",
"    Blood Energy Neutralizing Battery",
"    Dark Blood Energy Neutralizing Battery",
"    Energy Neutralizing Battery",
"    Sansha Energy Neutralizing Battery",
"    True Sansha Energy Neutralizing Battery",
"<html> <b>Hybrid Battery</b> </html>",
"    Large Blaster Battery",
"    Serpentis Large Blaster Battery",
"    Shadow Large Blaster Battery",
"    Medium Blaster Battery",
"    Serpentis Medium Blaster Battery",
"    Shadow Medium Blaster Battery",
"    Small Blaster Battery",
"    Serpentis Small Blaster Battery",
"    Shadow Small Blaster Battery",
"    Large Railgun Battery",
"    Serpentis Large Railgun Battery",
"    Shadow Large Railgun Battery",
"    Medium Railgun Battery",
"    Serpentis Medium Railgun Battery",
"    Shadow Medium Railgun Battery",
"    Small Railgun Battery",
"    Serpentis Small Railgun Battery",
"    Shadow Small Railgun Battery",
"<html> <b>Laser Battery</b> </html>",
"    Large Pulse Laser Battery",
"    Blood Large Pulse Laser Battery",
"    Dark Blood Large Pulse Laser Battery",
"    Sansha Large Pulse Laser Battery",
"    True Sansha Large Pulse Laser Battery",
"    Medium Pulse Laser Battery",
"    Blood Medium Pulse Laser Battery",
"    Dark Blood Medium Pulse Laser Battery",
"    Sansha Medium Pulse Laser Battery",
"    True Sansha Blood Medium Pulse Laser Battery",
"    Small Pulse Laser Battery",
"    Blood Small Pulse Laser Battery",
"    Dark Blood Small Pulse Laser Battery",
"    Sansha Small Pulse Laser Battery",
"    True Sansha Blood Small Pulse Laser Battery",
"    Large Beam Laser Battery",
"    Blood Large Beam Laser Battery",
"    Dark Blood Large Beam Laser Battery",
"    Sansha Large Beam Laser Battery",
"    True Sansha Blood Large Beam Laser Battery",
"    Medium Beam Laser Battery",
"    Blood Medium Beam Laser Battery",
"    Dark Blood Medium Beam Laser Battery",
"    Sansha Medium Beam Laser Battery",
"    True Sansha Blood Medium Beam Laser Battery",
"    Small Beam Laser Battery",
"    Blood Small Beam Laser Battery",
"    Dark Blood Small Beam Laser Battery",
"    Sansha Small Beam Laser Battery",
"    True Sansha Blood Small Beam Laser Battery",
"<html> <b>Missile Battery</b> </html>",
"    Cruise Missile Battery",
"    Dread Guristas Cruise Missile Battery",
"    Guristas Cruise Missile Battery",
"    Torpedo Battery",
"    Dread Guristas Torpedo Battery",
"    Guristas Torpedo Battery",
"    Citadel Torpedo Battery",
"    Dread Guristas Citadel Torpedo Battery",
"    Guristas Citadel Torpedo Battery",
"<html> <b>Projectile Battery</b> </html>",
"    Large AutoCannon Battery",
"    Angel Large AutoCannon Battery",
"    Domination Large AutoCannon Battery",
"    Medium AutoCannon Battery",
"    Angel Medium AutoCannon Battery",
"    Domination Medium AutoCannon Battery",
"    Small AutoCannon Battery",
"    Angel Small AutoCannon Battery",
"    Domination Small AutoCannon Battery",
"    Large Artillery Battery",
"    Angel Large Artillery Battery",
"    Domination Large Artillery Battery",
"    Medium Artillery Battery",
"    Angel Medium Artillery Battery",
"    Domination Medium Artillery Battery",
"    Small Artillery Battery",
"    Angel Small Artillery Battery",
"    Domination Small Artillery Battery"

所以这直接来自我原来的JListJTree应如下所示: 包含“html”标记的行应该是PARENT节点,而一个whit缩进应该是CHILD

所以看起来应该是这样的:

  • 盾牌硬币&lt;父
  • &gt;&gt;&gt;弹道偏转阵列&lt;子
  • &gt;&gt;&gt;&gt; Explosion Dampening Array&lt;子

我试过把它放在一个文件中并解析它并打印出这样的节点:

public class Env {
    public static void main(String[] args)
    {           
        try {
            Scanner input   = new Scanner(new File("data.txt"));
            String line;
            String sub;
            while(input.hasNextLine())
            {
                line = input.nextLine();

                String subline = line.replace('"', ' ').substring(11).replace("</b> </html> ,", " ").trim().replace(" ", "_");
                if(line.contains("<html>")){
                    System.out.println("DefaultMutableTreeNode " + subline + " = new DefaultMutableTreeNode(" + line.replace(",", " ").trim() + ");");
                } 
            }

        } catch (Exception e) {}
    }
}

在我写完代码之前,有一种更简单的方法可以做到这一点。

2 个答案:

答案 0 :(得分:2)

我只能想到使用正则表达式“简化”String替换,但即使这是一个意见问题。

import java.util.Scanner;

public class TestFoo {
   private static final String FILE_NAME = "data.txt";
   private static final String REGEX = "</?html>|</?b>|\"|,";

   public static void main(String[] args) {
      Scanner scan = new Scanner(new File(FILE_NAME));
      while (scan.hasNextLine()) {
         String line = scan.nextLine();
         String newLine = line.replaceAll(REGEX, "").trim();
         if (line.contains("html")) {
            System.out.println("Default Mut: " + newLine);
         } else {
            System.out.println("\t" + newLine);
         }
      }
   }
}

你当然需要try / catch,不应该忽略catch块。这也将删除引号内的逗号,因此不太理想。有可供在线下载的Java库,可以非常干净地解析逗号分隔的文件。

答案 1 :(得分:0)

没有短暂的方式 通过为子节点添加else来解析数据,如下所示解决问题。