Below you will find the Java code for creating a simple Swing tree application.
Swing tree
//package swing; //import statements import javax.swing.*; import javax.swing.tree.*; import java.util.*; import java.awt.*; /** * SimpleTreeFrame.java * This class shows how to create a simple JTree * using DefaultMutableTreeNode */ public class SimpleTreeFrame extends JFrame { public static final String RPG = "Role Playing Games"; public static final String FPS = "First Person Shooters"; public static final String SP = "Sports"; public static final String GAMES = "GAMES"; //JTree tree private JTree m_simpleTree; //Tree root node private DefaultMutableTreeNode m_rootNode; public SimpleTreeFrame() { super("Simple Tree Demo"); //creating tree nodes createTreeNodes(); //creating JTree by passing the root node m_simpleTree = new JTree(m_rootNode); //Add the JTree to the scroll pane to handle tree scrolling JScrollPane scrollPane = new JScrollPane(m_simpleTree); //add the scroll pane to the frame getContentPane().add(scrollPane); } /** * createTreeNodes * This function creates tree nodes */ private void createTreeNodes() { //creating the root node m_rootNode = new DefaultMutableTreeNode(GAMES); //creating different domain nodes DefaultMutableTreeNode rpgNode = new DefaultMutableTreeNode(RPG); DefaultMutableTreeNode fpsNode = new DefaultMutableTreeNode(FPS); DefaultMutableTreeNode spNode = new DefaultMutableTreeNode(SP); //get the dummy data vector Vector data = getDummyData(); //Enumerating the vector, create the machine nodes by passing MachineData //as UserObject and add the nodes under proper domain for (Enumeration enum = data.elements(); enum.hasMoreElements();) { GameData macData = (GameData) (enum.nextElement()); if (macData.getGameDomain().equals(RPG)) { rpgNode.add(new DefaultMutableTreeNode(macData)); } else if (macData.getGameDomain().equals(FPS)) { fpsNode.add(new DefaultMutableTreeNode(macData)); } else if (macData.getGameDomain().equals(SP)) { spNode.add(new DefaultMutableTreeNode(macData)); } } //adding domain nodes to the organization root node m_rootNode.add(rpgNode); m_rootNode.add(fpsNode); m_rootNode.add(spNode); } /** * getDummyData * This function creates Vector of dummy GameData objects and returns it */ private Vector getDummyData() { Vector dummyMacData = new Vector(10, 10); dummyMacData.addElement( new GameData( new Integer(100), "World of Warcraft", "PC", RPG)); dummyMacData.addElement( new GameData( new Integer(105), "Guild Wars", "PC", RPG)); dummyMacData.addElement( new GameData( new Integer(110), "Fable", "XBOX", RPG)); dummyMacData.addElement( new GameData( new Integer(106), "EverQuest", "PC", RPG)); dummyMacData.addElement( new GameData( new Integer(302), "Fifa", "ALL", SP)); dummyMacData.addElement( new GameData( new Integer(303), "ISSoccer", "All", SP)); dummyMacData.addElement( new GameData( new Integer(305), "Tennis", "All", SP)); dummyMacData.addElement( new GameData( new Integer(504), "Halo", "XBOX", FPS)); dummyMacData.addElement( new GameData( new Integer(505), "Quake", "PC", FPS)); dummyMacData.addElement( new GameData( new Integer(104), "Halflife", "PC", FPS)); return dummyMacData; } /** * GameData.java * The data object to store the Game Information */ public class GameData { private Integer m_gmCode; private String m_gmName; private String m_gmFormat; private String m_gmDomain; public GameData() { } public GameData( Integer gmCode, String gmName, String gmFormat, String gmDomain) { m_gmCode = gmCode; m_gmName = gmName; m_gmFormat = gmFormat; m_gmDomain = gmDomain; } public Integer getGameCode() { return m_gmCode; } public String getGameName() { return m_gmName; } public String getGameIP() { return m_gmFormat; } public String getGameDomain() { return m_gmDomain; } public void setGameCode(Integer gmCode) { m_gmCode = gmCode; } public void setGameName(String gmName) { m_gmName = gmName; } public void setGameIP(String gmFormat) { m_gmFormat = gmFormat; } public void setGameDomain(String gmDomain) { m_gmDomain = gmDomain; } public String toString() { return ( new String( m_gmCode + ": " + m_gmName + " (" + m_gmFormat + ") " )); } } /** * main */ public static void main(String[] arg) { SimpleTreeFrame m = new SimpleTreeFrame(); m.setVisible(true); m.setSize(new Dimension(450, 300)); m.validate(); } }