Search code examples
javaswingeventsjbuttonjtree

Event handling in Java (JTree + JButton)


private void createEvents() 
{
    menuFileExit.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent arg0) 
        {
            System.exit(0);
        }
    });
    ////// Events on tree selection
    jtStoryViewer.addTreeSelectionListener(new TreeSelectionListener() 
    {
        public void valueChanged(TreeSelectionEvent arg0) 
        {
            DefaultMutableTreeNode selection = (DefaultMutableTreeNode) jtStoryViewer.getLastSelectedPathComponent();
            Object nodeObject = selection.getUserObject();



            ////// Checks if selected node is a String (only story title is a string)
            if(selection.getUserObject().getClass().getName() == "java.lang.String" )
            {
                tfTitle.setText(nodeObject.toString());


                ////// Action listener for Change Button
                btnChange.addActionListener(new ActionListener() 
                {
                    ////// Title text swap
                    public void actionPerformed(ActionEvent arg0) 
                    {
                        selection.setUserObject(tfTitle.getText());
                        ((DefaultTreeModel)jtStoryViewer.getModel()).nodeChanged(selection);
                    }
                });
            }
            ///// checks if the object is a chapter object
            if(selection.getUserObject().getClass().getName() == "ISW.common.Chapter")
            {
                Chapter chapter = (Chapter) selection.getUserObject();
                tfTitle.setText(chapter.toString());



                ////// Action listener for Change Button
                btnChange.addActionListener(new ActionListener() 
                {
                    ////// Title text swap
                    public void actionPerformed(ActionEvent arg0) 
                    {
                        chapter.setTitle(tfTitle.getText());
                        ((DefaultTreeModel)jtStoryViewer.getModel()).nodeChanged(selection);

                    }
                });
            }


        }
    }); 
}

visualization of the problem

I am using JTree to display and modify some objects. I added a TreeSelectionListener to get the object data on selection. For now I want to be able to change the title of an object, it works fine on first selection on the tree , I change the value in the text box and the "Change" button works just fine, but when I move on to next objects, the change button also modifies the value of all previously selected objects.

I guess it is caused due to my improper usage of the ActionListeners but I can't tell for sure and at this point I'm stuck.

Will be grateful for any hints.


Solution

  • Don't keep adding an ActionListener to the btnChange JButton within the TreeSelectionListener#valueChanged method.

    This will cause the button to call EVERY ActionListener you have previously

    Instead, give the btnChange a single ActionListener, when clicked, can act on the currently selected node (by checking the JTree it self). You could have the TreeSelectionListener#valueChanged method enable or disable the btnChange based on the validity of the selection

    Also, if(selection.getUserObject().getClass().getName() == "ISW.common.Chapter") isn't how String comparison is done in Java, instead you should use something more like if("ISW.common.Chapter".equals(selection.getUserObject().getClass().getName()))