Yurttas/PL/OOL/Java/F/05/01/05/02/SolarSystem00.java

From ZCubes Wiki
Revision as of 14:35, 7 November 2013 by MassBot1 (talk | contribs) (Created page with "<syntaxhighlight lang="java" line start="1" enclose="div">import javax.swing.*; import javax.swing.tree.DefaultMutableTreeNode; import java.util.*; public class SolarSystem0...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
 1import javax.swing.*;
 2import javax.swing.tree.DefaultMutableTreeNode;
 3
 4import java.util.*;
 5
 6public class SolarSystem00 {
 7
 8  public static void main(String args[]) {
 9    // Data to initialize the components:
10    final int NAME     = 0;    // Index constants for accessing
11    final int ORBITING = 3;    // data in the following arrays.
12
13    Object[] columnNames =
14      {"Planet", "Orbit (km)",          "Diameter (km)",    "Orbiting"};
15
16    Object[][] planetaryData = {
17      {"Sun",     null,                 new Float(1390000), null   },
18      {"Mercury", new Float(57910000),  new Float(4880),    "Sun"  },
19      {"Venus",   new Float(108200000), new Float(12103.6), "Sun"  },
20      {"Earth",   new Float(149600000), new Float(12756.3), "Sun"  },
21      {"Moon",    new Float(384400),    new Float(3476),    "Earth"},
22      {"Mars",    new Float(227940000), new Float(6794),    "Sun"  },
23      {"Phobos",  new Float(9378),      new Float(22.2),    "Mars" },
24      {"Deimos",  new Float(23459),     new Float(12.6),    "Mars" },
25    };  // The last column indicates a parent-child relationship.
26
27    // Build tree nodes based on data.
28    Map namesToNodes = new HashMap();  // mapping names to nodes.
29
30    for(int i=0; i<planetaryData.length; i++) {    // For all planets...
31      Object[] planetProfile = planetaryData[i];       // lookup data
32
33      DefaultMutableTreeNode newNode =                 // make new node
34        new DefaultMutableTreeNode(planetProfile[NAME]);
35
36      namesToNodes.put(planetProfile[NAME], newNode);  // store node
37
38      DefaultMutableTreeNode parent =
39        (DefaultMutableTreeNode) namesToNodes.get(planetProfile[ORBITING]);   // find parent
40
41      if(parent!=null) parent.add(newNode);         // add to parent
42    }
43
44    // Find node for "Sun", which will be used as the root of the JTree
45    DefaultMutableTreeNode rootNode =
46      (DefaultMutableTreeNode) namesToNodes.get("Sun");
47
48    // Construct and display the GUI:
49    JTree tree = new JTree(rootNode);
50    JTable table = new JTable(planetaryData, columnNames);
51
52    JScrollPane scrollableTree  = new JScrollPane(tree);
53    JScrollPane scrollableTable = new JScrollPane(table);
54
55    JSplitPane windowContents =              // Split window contents 
56      new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,  // horizontally,
57                     scrollableTree,               // place tree left,
58                     scrollableTable);             // place table right.
59
60    JFrame mainWindow = new JFrame("Illustrating complex components");
61    mainWindow.setContentPane(windowContents);
62
63    mainWindow.pack();
64    mainWindow.setVisible(true);
65  }
66
67}