Results 1 to 2 of 2
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default TreeListener getting no response?

    I've adapted this from the Java Tutorials example..

    But it doesn't seem to work! Have I set up the TreeListener correctly?

    This code recreates my Product Price Tree once a Price has been added to a Product in the Product Creator. I want to be able to edit a Price too, so I need to be able to select a Price node from the Tree and then press "Edit" to edit that Price.

    The Prices are added fine but when I select anything in the Tree nothing is printed to output although I have many System.out.prints to help me recognise what is what.

    Java Code:
        private void updatePriceTree() {
            DefaultMutableTreeNode category = null;
            DefaultMutableTreeNode subcategory = null;
            priceTreeRoot.removeAllChildren();
            for (Products.Product.ProductPrices p:currentProduct.getPrices()) {
                category = new DefaultMutableTreeNode(p.getPriceDescription());
                priceTreeRoot.add(category);
                subcategory = new DefaultMutableTreeNode(String.format("Price: %.2f",p.getPrice()));
                category.add(subcategory);
                subcategory = new DefaultMutableTreeNode(String.format("Serving: %.3fKg",p.getServingSize()));
                category.add(subcategory);
                subcategory = new DefaultMutableTreeNode("Sold by serving: " + p.isPricePerKg());
                category.add(subcategory);
            }
            priceTreeModel.setRoot(priceTreeRoot);
            productPriceTree.setModel(priceTreeModel);
            productPriceTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
            productPriceTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
                public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                            evt.getPath().getLastPathComponent();
                    System.out.println(node);
                    if (node == null) {
                        System.out.println("Node is null");
                        return;
                    }
                    if (node.isLeaf()) {
                        System.out.println("This is a leaf");
                    }
                    if (node.isRoot()) {
                        System.out.println("This is a root");
                    }
                }
            });
        }
    Edit:
    I just noticed another problem in the following line of code:
    productPriceTree.getSelectionModel().setSelectionM ode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    This doesn't seem to be having any effect either as I am able to CTRL+Click and select multiple nodes.
    Last edited by ozzyman; 04-28-2011 at 06:25 PM.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    I've got it responding now, but the strange thing is my code is exactly the same as before :S.

    I used Netbeans IDE to right-click the JTree in design view and added the valueChanged listener from there - but the code is exactly the same and the only two small differences are these:

    Netbeans puts an outside method inside the actionlistener body like this:
    Java Code:
            productPriceTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
                public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                    productPriceTreeValueChanged(evt);
                }
            });
    The listener is now added in the 'initComponents()' method called in the constructor, where as before I had written it in my own 'initFields()' method called in the constructor.

Similar Threads

  1. response.setContentType
    By bbq in forum Java Servlet
    Replies: 3
    Last Post: 01-17-2012, 05:37 PM
  2. Replies: 0
    Last Post: 04-07-2011, 03:09 PM
  3. Response code from URL
    By sherlock in forum New To Java
    Replies: 1
    Last Post: 07-01-2010, 09:52 PM
  4. Not getting a response for query
    By Kelly in forum JDBC
    Replies: 3
    Last Post: 04-20-2010, 03:41 AM
  5. MDB/JMS Request/Response
    By rkands in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 03-04-2010, 02:38 PM

Posting Permissions

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