Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Swing Code Examples

Using JTree

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
Using JTree import javax.swing.tree.*; import javax.swing.*; import java.awt.*; public class TreeConstruct1 extends JFrame { public static void main(String[] args) { TreeConstruct obj=new TreeConstruct(); obj.setSize(350,500); obj.setVisible(true); } public TreeConstruct1() { try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch(Exception exception) { exception.printStackTrace(); } JPanel panel =new JPanel(); panel.setLayout(new FlowLayout(FlowLayout.LEFT)); DefaultMutableTreeNode top=new DefaultMutableTreeNode("Library"); DefaultMutableTreeNode branch1=new DefaultMutableTreeNode("Comics"); DefaultMutableTreeNode branch2=new DefaultMutableTreeNode("History"); DefaultMutableTreeNode branch3=new DefaultMutableTreeNode("Scientific"); //adding to the topmost node top.add(branch1); top.add(branch2); top.add(branch3); //adding to the First branch DefaultMutableTreeNode node1_b1=new DefaultMutableTreeNode("Tom and Jerry"); DefaultMutableTreeNode node2_b1=new DefaultMutableTreeNode("Simpsons"); branch1.add(node1_b1); branch1.add(node2_b1); //adding to the Second branch DefaultMutableTreeNode node1_b2=new DefaultMutableTreeNode("The Great History of Aruna Kumar Reddy"); DefaultMutableTreeNode node2_b2=new DefaultMutableTreeNode("Chanakya"); DefaultMutableTreeNode node3_b2=new DefaultMutableTreeNode("Changhiz Khan"); branch2.add(node1_b2); branch2.add(node2_b2); branch2.add(node3_b2); //adding to the Third branch DefaultMutableTreeNode node1_b3=new DefaultMutableTreeNode("Physical"); DefaultMutableTreeNode node2_b3=new DefaultMutableTreeNode("Biological"); DefaultMutableTreeNode n1_node2_b3=new DefaultMutableTreeNode("Animal Science"); DefaultMutableTreeNode n2_node2_b3=new DefaultMutableTreeNode("Plant Science"); node2_b3.add(n1_node2_b3); node2_b3.add(n2_node2_b3); DefaultMutableTreeNode node3_b3=new DefaultMutableTreeNode("Chemical"); branch3.add(node1_b3); branch3.add(node2_b3); branch3.add(node3_b3); ImageIcon icon=new ImageIcon("abook.gif"); JTree tree=new JTree(top,true); tree.setToolTipText(" and "); panel.add(tree); getContentPane().add(panel); } }