*State the use of JTree in Swing.
1- The JTree class is used to display the tree structured data or hierarchical data.
2- JTree Is complex component .
3- It has root node at the top most which is parent for all nodes in the tree
4- it inherits JComponent class.
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.*;
public class treeexample {
treeexample() {
JFrame f = new JFrame();
DefaultMutableTreeNode style = new DefaultMutableTreeNode("Style");
DefaultMutableTreeNode color = new DefaultMutableTreeNode("color");
DefaultMutableTreeNode font = new DefaultMutableTreeNode("Font");
style.add(color);
style.add(font);
DefaultMutableTreeNode red = new DefaultMutableTreeNode("red");
DefaultMutableTreeNode blue = new DefaultMutableTreeNode("blue");
DefaultMutableTreeNode black = new DefaultMutableTreeNode("black");
DefaultMutableTreeNode green = new DefaultMutableTreeNode("green");
color.add(red);
color.add(blue);
color.add(black);
color.add(black);
color.add(green);
JTree jt = new JTree(style);
f.add(jt);
f.setSize(900, 900);
f.setVisible(true);
}
public static void main(String args[]) {
new treeexample();
}
}
output:
No comments:
Post a Comment