Results 1 to 8 of 8
  1. #1
    Fluffy93 is offline Member
    Join Date
    Aug 2014
    Posts
    32
    Rep Power
    0

    Default Display more information when node is clicked in a Jtree?

    I'm new to java and I'm currently trying to make a program that uses a JTree that implements a properties file.

    I have the Keys set to the nodes but I want it so that when the node is clicked it will retrieve the keys value from the prop file.

    I did have some code for this initially but I as it did not work at the time and was causing issues to my project I got rid of it

    I have my code for the jtree,the listener and the prop file

    Tree
    Java Code:
                    JPanel panel_1 = new JPanel();
    		panel_1.setBackground(Color.WHITE);
    		panel_1.setSize(new Dimension(22, 0));
    		scrollPane.setViewportView(panel_1);
    		Properties properties = new Properties();
    		try {
    
    			//properties file - get the data in the file
    			String filename = "GitCommands.properties";
    			Properties p = properties;
    			FileInputStream fileInputStream = new FileInputStream(filename); 
    			//load properties file  
    			properties.load(fileInputStream);
    			panel_1.setLayout(new MigLayout("", "[][][][][][][]", "[322px,grow,fill][][][][][]"));
    
    			//sets up a scroll pane to view all of the tree
    			JScrollPane scrollpane = new JScrollPane();
    			//frame.getContentPane().add(scrollpane, "Center");
    			panel_1.add(scrollpane, "cell 0 5,alignx left,aligny top");
    
    
    			//what will happen when the user closes the program
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    
    			//sets new jtree
    			tree = new JTree(p);
    
    
    			//Where the tree is initialised:
    			tree.getSelectionModel().setSelectionMode
    			(TreeSelectionModel.SINGLE_TREE_SELECTION);
    
    
    			//Set the icon for leaf nodes.
    			ImageIcon leafIcon = createImageIcon("star.png");
    			if (leafIcon != null) {
    				DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
    
    
    				renderer.setLeafIcon(leafIcon);
    				tree.setCellRenderer(renderer);
    
    
    			} else {
    				System.err.println("Leaf icon missing - using default icon.");
    			}
    
    
    
    			//Listen for when the selection changes.
    			tree.addTreeSelectionListener(this);
    
    
    			panel_1.add(tree, "cell 6 0 1 3");
    			tree.setAlignmentX(Component.RIGHT_ALIGNMENT);
    			tree.setBounds(new Rectangle(0, 0, 40, 0));
    			tree.setToolTipText("Click a command");
    
    
    			//allows the root of the tree to be shown
    			tree.setRootVisible(true);
    
    			//it will be visible
    			frame.setVisible(true);
    		}
    		catch (Exception e) {}
    ////Highlight the node selected and print it out to the console to ensure the listener works
    Java Code:
    	@Override
    	public void valueChanged(TreeSelectionEvent argO) {
    		// TODO Auto-generated method stub
    		TreeNode selectedNode =   
    				(TreeNode)tree.getLastSelectedPathComponent();   
    		System.out.println(selectedNode);  
            
    	}
    I know I need to use something like this:
    Java Code:
    String stValue = GitCommands.getProperty(" ");
    props file sample - Key = value

    Java Code:
    add = Adds files changes in your working directory to your index. Example: git add .
    
    rm = Removes files from your index and your working directory so they will not be tracked. Example: git rm filename
    Is there a way of doing this? Been trying for a while now but to no prevail. The image is what it currently looks like
    Display more information when node is clicked in a Jtree?-helpsystem-3.png
    Last edited by Fluffy93; 08-27-2014 at 01:26 PM. Reason: adding content

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Display more information when node is clicked in a Jtree?

    You forget to post what you have been trying. I'm also very unclear on what exactly you are having trouble with. You have a key that you successfully get from the JTree (which is basically the hard part!)... and then what? You use this key to do what? You say "get a value from a properties file", but I see no properties file, no code that has anything to do with a properties file, no nothing.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Fluffy93 is offline Member
    Join Date
    Aug 2014
    Posts
    32
    Rep Power
    0

    Default Re: Display more information when node is clicked in a Jtree?

    Ah my apologies! I'll put up the code for the props file now and add some other stuff :)

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Display more information when node is clicked in a Jtree?

    Still incredibly vague what you are having trouble with. I have no idea what this "GitCommands" is, I guess that is your own class.

    Is the problem perhaps that you don't know how to get the label you see visually in that picture as a String object, to be able to fetch the matching value from the properties?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Fluffy93 is offline Member
    Join Date
    Aug 2014
    Posts
    32
    Rep Power
    0

    Default Re: Display more information when node is clicked in a Jtree?

    Git commands is just the name of my properties file - it holds key values of actual GIT commands

    My problem is is that I have gotten the keys from the prop file into my Jtree completely fine however - I can't retrieve the selected values to go with them.

    E.g - I click "prune" in Jtree - it then gets the corresponding value of key "prune" from the prop file and displays it in a panel next to the tree.

    Does that make it clearer for you? Thanks for your time too.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Display more information when node is clicked in a Jtree?

    Git commands is just the name of my properties file
    Well then you are lying in what you posted as you made it a class name with a static method call.

    And no it does not make it any clearer, because:

    I can't retrieve the selected values to go with them.
    That is not a problem description. I can't help you with that, you don't say what you tried and what makes you say that "you can't do it". What does that even mean? You don't know something? You tried something and you got an error?

    -What- is stopping you? What are you doing? What don't you know? What did you try? What is happening and what do you expect to happen?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Fluffy93 is offline Member
    Join Date
    Aug 2014
    Posts
    32
    Rep Power
    0

    Default Re: Display more information when node is clicked in a Jtree?

    Basically I have no idea how to make this work and I honestly cant remember the stuff I have tried as I've been stuck for days :(

    Also I have no idea how I've used a static method call for it exactly?

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Display more information when node is clicked in a Jtree?

    I recommend you go thru the tutorial --> How to Use Trees (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Then you should try some simple examples to make certain you understand the concepts. JTree, JTable, and similar
    classes make use of the Flyweight design pattern. It may help you better understand how it works. Read about it
    here --> Make your apps fly | JavaWorld.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. action to a node in a jtree
    By sniffer139 in forum AWT / Swing
    Replies: 4
    Last Post: 08-29-2014, 04:51 PM
  2. Multiple components as one node in JTree
    By Amnuriak in forum AWT / Swing
    Replies: 8
    Last Post: 11-04-2009, 03:47 PM
  3. Replies: 1
    Last Post: 06-22-2009, 03:39 AM
  4. How to add JCheckbox as a node in JTree
    By shajuantony in forum New To Java
    Replies: 0
    Last Post: 04-09-2009, 08:19 AM
  5. Node selection in jtree
    By Preethi in forum AWT / Swing
    Replies: 4
    Last Post: 06-20-2008, 12:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •