So i am used to building up a JTree by adding DefaultMutableTreeNode‘s to its model but right now i have a lot of entries that belong to 1 JTree is there a shorter way to do this maybe like you do whit a JList where you can just specify a 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"
So this is directly out of my old JList. The JTree should look like this:
Line that contains “html” tag should be a PARENT node and the one whit indent should be CHILD
So it should look like this:
- Shield Hardners < Parent
- >>>Ballistic Deflection Array < Child
- >>>Explosion Dampening Array < Child
I have tried by puting this in a file and parsing it and print out the nodes like so:
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) {}
}
}
Before i finish writing that code is there a simpler way to do this.
I can only think of using regular expressions to “simplify” the String replace, but even that’s a matter of opinion.
You’ll need try/catch of course, and shouldn’t ignore the catch block. This will also remove commas inside of the quotes and so is not ideal. There are Java libraries available online for download that allow very clean parsing of comma-delimited files.