By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,570 Members | 1,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,570 IT Pros & Developers. It's quick & easy.

slider that I can resize circle

P: 1
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


Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JOptionPane;
  7. import javax.swing.JPanel;
  8. import javax.swing.JScrollBar;
  9. import javax.swing.JScrollPane;
  10. import javax.swing.JSlider;
  11. import javax.swing.JTextField;
  12. import javax.swing.JTree;
  13. import javax.swing.event.*;
  14. import javax.swing.tree.DefaultMutableTreeNode;
  15. import javax.swing.tree.DefaultTreeModel;
  16. import javax.swing.tree.MutableTreeNode;
  17. import javax.swing.tree.TreeNode;
  18. import javax.swing.tree.TreePath;
  19.  
  20. public class HirvaTree extends JFrame implements ActionListener {
  21.  
  22.  
  23.     private static final long serialVersionUID = 1L;
  24.  
  25.     public static int WIDTH = 400;
  26.  
  27.     public static int HEIGHT = 400;
  28.  
  29.     public static String TITLE = "Trees";
  30.  
  31.     Container frameContainer;
  32.  
  33.     JTextField TF = new JTextField(20);
  34.  
  35.     JScrollPane scrollPane = new JScrollPane();
  36.  
  37.     JScrollBar scrollbar,scrollbar1 = new JScrollBar();
  38.  
  39.     JSlider slider = new JSlider(JSlider.HORIZONTAL, 1, 100, 37);
  40.  
  41.     JButton jb= new JButton();
  42.  
  43.     JFrame frame= new JFrame("trees");
  44.  
  45.   // Renderer renderer = new Renderer();
  46.  
  47.    JTree mytree = new JTree();
  48.  
  49.    JPanel panel = new JPanel();
  50.  
  51.    public static DefaultTreeModel model;
  52.  
  53.    public static TreePath path;
  54.  
  55.    public static JTree tree;
  56.  
  57.    public static DefaultMutableTreeNode nNode;
  58.  
  59.    public static MutableTreeNode node;
  60.  
  61.    TreeHandler treeHand = new TreeHandler();
  62.  
  63.  
  64.  
  65.    //constructor
  66.    public HirvaTree()
  67. {    DefaultMutableTreeNode treenode = getTree();
  68.  
  69.     model = new DefaultTreeModel(treenode);
  70.  
  71.     mytree = new JTree(model);
  72.  
  73.     frame.setLayout(new FlowLayout(FlowLayout.CENTER));
  74.  
  75.     // add button
  76.     jb = new JButton("Add");
  77.     jb.addActionListener(this);
  78.     frame.add(jb);
  79.  
  80.     // Remove button
  81.     jb=new JButton("Remove");
  82.     jb.addActionListener(this);
  83.     frame.add(jb);
  84.  
  85.     // delete all button
  86.     jb=new JButton("Delete All");
  87.     jb.addActionListener(this);
  88.     frame.add(jb);
  89.  
  90.     //rename button
  91.     jb=new JButton("Rename Node");
  92.     jb.addActionListener(this);
  93.     frame.add(jb);
  94.  
  95.     // add textfield
  96.     frame.add(TF);
  97.  
  98.     // scrollbar
  99.    scrollbar = new JScrollBar(JScrollBar.HORIZONTAL,0,1,0,400);
  100.  
  101.    scrollbar1 = new JScrollBar(JScrollBar.VERTICAL,0,1,0,400);
  102.  
  103.     // frame size and visible
  104.     frame.setSize(WIDTH,HEIGHT);
  105.  
  106.     frame.setVisible(true);
  107.  
  108.     frame.add(panel);
  109.  
  110.     panel.add(scrollbar);
  111.  
  112.     panel.add(scrollbar1);
  113.  
  114.     mytree.addTreeSelectionListener(treeHand);
  115.  
  116.     panel.add(mytree);
  117.  
  118.     frame.add(slider);
  119. }
  120. public void layoutComponents() {
  121.  
  122.     frameContainer=getContentPane();
  123.  
  124.        scrollPane.getViewport().add(mytree);
  125.  
  126.     frameContainer.add("Left", scrollPane);
  127.  
  128.     frameContainer.add("Center", TF);
  129.  
  130. }
  131.  
  132. public void actionPerformed(ActionEvent ae){
  133.  
  134.     String str= ae.getActionCommand();
  135.     //add button logic
  136.     if (str.equals("Add"))
  137.     {
  138.         DefaultMutableTreeNode  root = new DefaultMutableTreeNode(TF.getText());
  139.         DefaultMutableTreeNode a = (DefaultMutableTreeNode)treeHand.selectedNode;
  140.         a.add(root);
  141.         mytree.updateUI();
  142.          panel.add(mytree);
  143.          JOptionPane.showMessageDialog(null, "Node "+TF.getText()+" is add to the tree");
  144.  
  145.      }
  146.  
  147. //remove button logic
  148.     if(str.equals("Remove"))
  149.     {
  150.     DefaultMutableTreeNode selectedNode = getSelectedNode();
  151.     if (selectedNode != null)
  152.     model.removeNodeFromParent(selectedNode);
  153.         mytree.updateUI();
  154.         panel.add(mytree);
  155.          JOptionPane.showMessageDialog(null, "Node "+selectedNode+" is deleted from  tree");
  156.  
  157.     }
  158.  
  159.  
  160. //delete all button logic
  161.     if(str.equals("Delete All"))
  162.     {
  163.         DefaultMutableTreeNode a = (DefaultMutableTreeNode)treeHand.selectedNode;
  164.         a.removeAllChildren();
  165.         mytree.setRootVisible(false);
  166.         mytree.updateUI();
  167.     }
  168.  
  169. //remove buttion logic
  170.     if(str.equals("Rename Node"))
  171.     {
  172.             mytree.setEditable(true);
  173.             TreePath path = mytree.getSelectionPath();
  174.             mytree.startEditingAtPath(path);
  175.     }
  176.  
  177.  
  178. }
  179.  
  180. //to show default tree on the screen
  181.     private DefaultMutableTreeNode getTree() {
  182.         DefaultMutableTreeNode nNode = new DefaultMutableTreeNode("Jtree");
  183.  
  184.         DefaultMutableTreeNode colour = new DefaultMutableTreeNode("Colour");
  185.         nNode.add(colour);
  186.  
  187.         DefaultMutableTreeNode pink = new DefaultMutableTreeNode("Pink");
  188.         colour.add(pink);
  189.         DefaultMutableTreeNode yellow = new DefaultMutableTreeNode("Yellow");
  190.         colour.add(yellow);
  191.  
  192.         DefaultMutableTreeNode food = new DefaultMutableTreeNode("Food");
  193.         nNode.add(food);
  194.  
  195.         return nNode;
  196.       }
  197.  
  198.     public DefaultMutableTreeNode getSelectedNode() {
  199.         return (DefaultMutableTreeNode) mytree.getLastSelectedPathComponent();
  200. }
  201.  
  202.  
  203. //main class
  204.   public static void main(String[] args)
  205.     {
  206.         HirvaTree hi = new HirvaTree();
  207.     }
  208.  
  209. }
  210.  
  211.  
  212.  
  213. class TreeHandler implements TreeSelectionListener {
  214.     public TreeNode selectedNode;
  215.  
  216.     public void valueChanged(TreeSelectionEvent e) {
  217.         TreePath e1 = e.getPath();
  218.         Object[] e2 = e1.getPath();
  219.         selectedNode =(TreeNode)e2[e1.getPathCount()-1];                                                    // Node and convert
  220.         System.out.println("Current Selected Node :" + ((TreeNode)e2[e1.getPathCount()-1]));  // For infomration printing selected node's name.
  221.  
  222.         if(selectedNode.equals("circle"))
  223.         {
  224.             public void paint(Graphics a)
  225.             {
  226.                 a.drawOval(60,60,40,50);
  227.             }
  228.  
  229.         }
  230.  
  231.         /*if(selectedNode.equals("rectangel"))
  232.         {
  233.             //drawrect();
  234.             public void paint(Graphics g)
  235.             {
  236.                 g.drawoval(60,60,40,50);
  237.             }
  238.             System.out.println("node is Rectangle");
  239.         }*/
  240.  
  241. }
  242.  
  243. }
Oct 8 '09 #1
Share this Question
Share on Google+
1 Reply


Expert 10K+
P: 11,448
It would be better if you'd post a short example, not a large listing that uses a lot of different Swing components that obfuscate what it's all about: a JPanel that is supposed to draw a circle with a radius that can be changed by a JSlider. Now the reader has to plough through JTrees, JScrollbars, JButtons, JFrames, JScrollPanes and almost the entire collection of available components ...

kind regards,

Jos

ps. don't forget to use the code tags when you have to post Java code.
Oct 8 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.