473,698 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

slider that I can resize circle

1 New Member
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
1 3673
JosAH
11,448 Recognized Expert MVP
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

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

Similar topics

1
6865
by: Jason Charalambides | last post by:
I need to assign a slider to determine a percentage value for a certain variable in my program. I decided to use a slider so that the user will be able to manually set a value between 0.000... to 1.000 for that variable. The problem for me is how do I associate the slider to that variable? Do I need to write something specific within that or do I just give the slider control the name of the variable that it shall control? Thank you J.C.
0
1906
by: Brian Henry | last post by:
Ok I've never implemented a snap location before so I dont really know what im doing wrong here... anyways, I am making a custom slider control that takes dates as its values instead of integers... then taking that date range and finding dates specifiec between them (in a list of dates) and putting snap marks, so if you slide it near one of them it should snap to that tick, but that part i cant figure out. the rest seems ok so far... here...
1
2290
by: Israel | last post by:
The problem: I want to know, definitively when a slider loses focus after a user has started sliding and hasn't released the mouse yet. It appears that this is captured with the WM_ACTIVATEAPP message but this only goes to the form and I want it in the user control that maybe very much removed from the form's knowledge; i.e. the form may launch a 3rd party form that, upon a call back, decides to launch other form that has my user control...
7
4086
by: murrayatuptowngallery | last post by:
I have seen applets for mathematical functions with a mouse-operated slide-bar to vary a parameter in lieu of direct numeric entry. One waws online as Java I believe and the others were created by the Maple math package. I believe it can export them to various languages. Is such an entity possible and how difficult with PHP? Thank you.
20
3728
by: fatfatpopo | last post by:
Anyone who understand the codes below, can you please kindly explain to me by adding comments.. thanks in advance // <applet code="ImageSlider" width="400" height="400"></applet> import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*;
0
1212
by: Home Coder | last post by:
Below is a link to a web page that loads a Java applet (sorry in advance). In the applet, on the bottom there is a pink colored slider. I would like to use/buy an a web version of this slider. Do you know of any? I'll try to describe what makes this slider special. It's a horizontal slider, much like a scroll bar, but the end points are not fixed. You don't click on them, you drag either of them to lenghten or shorten the slider. This...
6
3099
by: cmgmyr | last post by:
Hello, I'm trying to figure out how to disable a slider through the onChange function. Here is what I have so far: new Control.Slider('handle','slider', { axis:'horizontal', sliderValue: 1, range: $R(1, 100), alignY:0,
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.