i have make a jtree program in java and if the node is circle then the circle can be shown in frame and i have to resize it with slider . plz help me i have done the tree program bt how to get selected node means its circle of any thing else i can't get it ..
someone plz help me
i have attached my program with this
someone plz help me
i have attached my program with this
Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class HirvaTree extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
public static int WIDTH = 400;
public static int HEIGHT = 400;
public static String TITLE = "Trees";
Container frameContainer;
JTextField TF = new JTextField(20);
JScrollPane scrollPane = new JScrollPane();
JScrollBar scrollbar,scrollbar1 = new JScrollBar();
JSlider slider = new JSlider(JSlider.HORIZONTAL, 1, 100, 37);
JButton jb= new JButton();
JFrame frame= new JFrame("trees");
// Renderer renderer = new Renderer();
JTree mytree = new JTree();
JPanel panel = new JPanel();
public static DefaultTreeModel model;
public static TreePath path;
public static JTree tree;
public static DefaultMutableTreeNode nNode;
public static MutableTreeNode node;
TreeHandler treeHand = new TreeHandler();
//constructor
public HirvaTree()
{ DefaultMutableTreeNode treenode = getTree();
model = new DefaultTreeModel(treenode);
mytree = new JTree(model);
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
// add button
jb = new JButton("Add");
jb.addActionListener(this);
frame.add(jb);
// Remove button
jb=new JButton("Remove");
jb.addActionListener(this);
frame.add(jb);
// delete all button
jb=new JButton("Delete All");
jb.addActionListener(this);
frame.add(jb);
//rename button
jb=new JButton("Rename Node");
jb.addActionListener(this);
frame.add(jb);
// add textfield
frame.add(TF);
// scrollbar
scrollbar = new JScrollBar(JScrollBar.HORIZONTAL,0,1,0,400);
scrollbar1 = new JScrollBar(JScrollBar.VERTICAL,0,1,0,400);
// frame size and visible
frame.setSize(WIDTH,HEIGHT);
frame.setVisible(true);
frame.add(panel);
panel.add(scrollbar);
panel.add(scrollbar1);
mytree.addTreeSelectionListener(treeHand);
panel.add(mytree);
frame.add(slider);
}
public void layoutComponents() {
frameContainer=getContentPane();
scrollPane.getViewport().add(mytree);
frameContainer.add("Left", scrollPane);
frameContainer.add("Center", TF);
}
public void actionPerformed(ActionEvent ae){
String str= ae.getActionCommand();
//add button logic
if (str.equals("Add"))
{
DefaultMutableTreeNode root = new DefaultMutableTreeNode(TF.getText());
DefaultMutableTreeNode a = (DefaultMutableTreeNode)treeHand.selectedNode;
a.add(root);
mytree.updateUI();
panel.add(mytree);
JOptionPane.showMessageDialog(null, "Node "+TF.getText()+" is add to the tree");
}
//remove button logic
if(str.equals("Remove"))
{
DefaultMutableTreeNode selectedNode = getSelectedNode();
if (selectedNode != null)
model.removeNodeFromParent(selectedNode);
mytree.updateUI();
panel.add(mytree);
JOptionPane.showMessageDialog(null, "Node "+selectedNode+" is deleted from tree");
}
//delete all button logic
if(str.equals("Delete All"))
{
DefaultMutableTreeNode a = (DefaultMutableTreeNode)treeHand.selectedNode;
a.removeAllChildren();
mytree.setRootVisible(false);
mytree.updateUI();
}
//remove buttion logic
if(str.equals("Rename Node"))
{
mytree.setEditable(true);
TreePath path = mytree.getSelectionPath();
mytree.startEditingAtPath(path);
}
}
//to show default tree on the screen
private DefaultMutableTreeNode getTree() {
DefaultMutableTreeNode nNode = new DefaultMutableTreeNode("Jtree");
DefaultMutableTreeNode colour = new DefaultMutableTreeNode("Colour");
nNode.add(colour);
DefaultMutableTreeNode pink = new DefaultMutableTreeNode("Pink");
colour.add(pink);
DefaultMutableTreeNode yellow = new DefaultMutableTreeNode("Yellow");
colour.add(yellow);
DefaultMutableTreeNode food = new DefaultMutableTreeNode("Food");
nNode.add(food);
return nNode;
}
public DefaultMutableTreeNode getSelectedNode() {
return (DefaultMutableTreeNode) mytree.getLastSelectedPathComponent();
}
//main class
public static void main(String[] args)
{
HirvaTree hi = new HirvaTree();
}
}
class TreeHandler implements TreeSelectionListener {
public TreeNode selectedNode;
public void valueChanged(TreeSelectionEvent e) {
TreePath e1 = e.getPath();
Object[] e2 = e1.getPath();
selectedNode =(TreeNode)e2[e1.getPathCount()-1]; // Node and convert
System.out.println("Current Selected Node :" + ((TreeNode)e2[e1.getPathCount()-1])); // For infomration printing selected node's name.
if(selectedNode.equals("circle"))
{
public void paint(Graphics a)
{
a.drawOval(60,60,40,50);
}
}
/*if(selectedNode.equals("rectangel"))
{
//drawrect();
public void paint(Graphics g)
{
g.drawoval(60,60,40,50);
}
System.out.println("node is Rectangle");
}*/
}
}
Comment