Results 1 to 10 of 10
  1. #1
    newmanity is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default [SOLVED] MouseListener / ActionListener in another class

    Hi.

    I am trying to put together a main application based on the JFrame class which contains different subwindows that are also themselves classes. One of the windows is based on the JTree class, and although I can write the listener code to respond to a mouse (or action) event from within the JTree class, I need to update info in the other windows from within the main class depending on the JTree node selected. Is it possible to have mouseListeners (and actionListeners) for this sub window JTree class in the main class so that I can trigger the various cross-window operations I need to perform? How would I do this?

    Here is a part of the sub window based on the JTree class with the listener inside it's own class:
    Java Code:
    public class NetworkTree extends JPanel implements TreeSelectionListener {
      private JTree tree;
      private static int PANEL_WIDTH=250;
      private static int PANEL_HEIGHT=500;
      private static String lineStyle = "Horizontal";
        
      public NetworkTree() {
        DefaultMutableTreeNode top = new DefaultMutableTreeNode(new Site(
                        1,1,"Network","",3));
        getSubNodes(top,0);
        tree = new JTree(top);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.addTreeSelectionListener(this);
        JScrollPane treeView = new JScrollPane(tree);
        add(treeView);
    
        tree.addMouseListener(new MouseAdapter() {
          public void mouseClicked(MouseEvent me) {
            newNodeSelected();
          }
        });
      }
      .....
      public void newNodeSelected() {
          JOptionPane.showMessageDialog(null,"Hello");
      }
      .....
    }
    Thanks for any help.
    Last edited by newmanity; 12-07-2009 at 04:02 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    your newNodeSelected method is public and can be called from anywhere. What your listener will need is a reference to the current object of the NetworkTree class, and that can easily be sent to the Listener class via a constructor parameter or a setter method.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Sometimes it may make sense to send listeners from another class.
    Java Code:
    [code]
    public class Central {
        public Central() {
            NetworkTree networkTree = new NetworkTree(tsl, ml);
            add networkTree to gui
        }
    
        private TreeSelectionListener tsl = new TreeSelectionListneer) {
            public void valueChanged(TreeSelectionEvent e) {
                code to report selection changes in networkTree
            }
        };
    
        private MouseListener ml = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                JOptionPane.showMessageDialog(null,"Hello");
            }
        };
    }
    
    
    public class NetworkTree extends JPanel {
        ...
    
        public NetworkTree(TreeSelectionListener tsl, MouseListener ml) {
            ...
            tree = new JTree(top);
            tree.getSelectionModel().setSelectionMode(
                    TreeSelectionModel.SINGLE_TREE_SELECTION);
            tree.addTreeSelectionListener(tsl);
            tree.addMouseListener(ml);
            ...
        }
    [/code]

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    compilable e.g.,

    Java Code:
    import javax.swing.JFrame;
    
    public class NetworkTreeTest {
      private static void createAndShowUI() {
        NetworkTree networkTree = new NetworkTree();
        NetworkTreeListener listener = new NetworkTreeListener();
        
        networkTree.addTreeSelectionListener(listener);
        listener.setTree(networkTree);
        
        JFrame frame = new JFrame("NetworkTreeTest");
        frame.getContentPane().add(networkTree);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.TreeSelectionModel;
    
    public class NetworkTree extends JPanel {
      private JTree tree;
      private static int PANEL_WIDTH = 250;
      private static int PANEL_HEIGHT = 500;
      private static String lineStyle = "Horizontal";
    
      public NetworkTree() {
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("Top");
        tree = new JTree(top);
        tree.getSelectionModel().setSelectionMode(
            TreeSelectionModel.SINGLE_TREE_SELECTION);
        JScrollPane treeView = new JScrollPane(tree);
        setLayout(new BorderLayout());
        add(treeView, BorderLayout.CENTER);
        createNodes(top);
        setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
      }
      
      public void addTreeSelectionListener(TreeSelectionListener tsl) {
        tree.addTreeSelectionListener(tsl);
      }
    
      public void newNodeSelected() {
        JOptionPane.showMessageDialog(null, "Hello");
      }
    
      // from the tutorials:
      // http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
      private void createNodes(DefaultMutableTreeNode top) {
        DefaultMutableTreeNode category = null;
        DefaultMutableTreeNode book = null;
    
        category = new DefaultMutableTreeNode("Books for Java Programmers");
        top.add(category);
    
        book = new DefaultMutableTreeNode(new BookInfo(
            "The Java Tutorial: A Short Course on the Basics", "tutorial.html"));
        category.add(book);
    
        book = new DefaultMutableTreeNode(
            new BookInfo("The Java Tutorial Continued: The Rest of the JDK",
                "tutorialcont.html"));
        category.add(book);
    
        // JFC Swing Tutorial
        book = new DefaultMutableTreeNode(new BookInfo(
            "The JFC Swing Tutorial: A Guide to Constructing GUIs",
            "swingtutorial.html"));
        category.add(book);
    
        // ...add more books for programmers...
    
        category = new DefaultMutableTreeNode("Books for Java Implementers");
        top.add(category);
    
        // VM
        book = new DefaultMutableTreeNode(new BookInfo(
            "The Java Virtual Machine Specification", "vm.html"));
        category.add(book);
    
        // Language Spec
        book = new DefaultMutableTreeNode(new BookInfo(
            "The Java Language Specification", "jls.html"));
        category.add(book);
      }
    
    }
    
    class BookInfo {
      private String title;
      private String address;
    
      public BookInfo(String title, String address) {
        this.title = title;
        this.address = address;
      }
    
      public String toString() {
        return title + ", " + address;
      }
    
    }

    Java Code:
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    
    public class NetworkTreeListener implements TreeSelectionListener {
      private NetworkTree networkTree;
      
      public void setTree(NetworkTree tree) {
        this.networkTree = tree;
      }
      
      public void valueChanged(TreeSelectionEvent e) {
        System.out.println(e.getNewLeadSelectionPath());
        networkTree.newNodeSelected();
      }
      
    }

  5. #5
    newmanity is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Wow...thanks guys!

    I implemented your code and it works. I will try to modify the code so that the NetworkTreeListener class' valueChanged method also calls a method in another window / class, and I'll get back to you tomorrow.

    Thanks again.

  6. #6
    newmanity is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Ok, Funbarable. I see how you've taken the treeListener outside of the tree and then linked them together in NetworkTreeTest, but what I want to be able to do is also call methods, based on this treeListener event, in classes other than the NetworkTree class.

    If, for example, I were to create an OtherWindow class like the following (and add an instance of it to the NetworkTreeTest content pane as you added NetworkTree networkTree), how could I call the setTexter method from the TreeListener event?

    Java Code:
    public class OtherWindow extends JPanel {
    
      private JLabel label;
      private JTextField texter;
    
      public OtherWindow() {
        JPanel p1 = new JPanel();
        label = new JLabel("This is the Network segment selected: ");
        texter = new JTextField(10);
        p1.add(label);
        p1.add(texter);
        add(p1);
      }  
        
      public void setTexter(String text) {
        texter.setText("This is the default text");
      }
    }
    This may be ignorant, but I've been trying to figure this out for hours.
    Thanks.
    Last edited by newmanity; 12-07-2009 at 03:30 AM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  8. #8
    newmanity is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Yes, I did notice that, but when I tried to modify your NetworkTreeListener class as in the following, it doesn't seem to be doing anything. It compiles fine, but doesn't change my text field...I just assumed that I couldn't do that, am I wrong?

    Java Code:
    public class NetworkTreeListener implements TreeSelectionListener {
      private NetworkTree networkTree;
      private OtherWindow oWin;
      
      public void setTree(NetworkTree tree) {
        this.networkTree = tree;
      }
     
      public void setOWin(OtherWindow win) {
        this.oWin = win;
      }
      
      public void valueChanged(TreeSelectionEvent e) {
        System.out.println(e.getNewLeadSelectionPath());
        JOptionPane.showMessageDialog(null,"it calls it");
        oWin.setTexter("text changed");
      }
    }
    Last edited by newmanity; 12-07-2009 at 03:53 AM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  10. #10
    newmanity is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Never mind...I'm an idiot....that worked alright, it was my setTexter method that wasn't doind anything!!! lol

    Thanks for everything....that really helps!

Similar Threads

  1. MouseListener & GUI
    By Suurisa in forum New To Java
    Replies: 2
    Last Post: 10-27-2009, 12:52 AM
  2. exception error in a ActionListener class
    By aserothbw in forum AWT / Swing
    Replies: 3
    Last Post: 09-22-2009, 12:59 PM
  3. exception error in a ActionListener class
    By aserothbw in forum AWT / Swing
    Replies: 6
    Last Post: 09-04-2009, 03:07 PM
  4. i need help for MouseListener
    By sfaxianovic in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 03:30 AM
  5. MouseListener
    By Aswq in forum New To Java
    Replies: 12
    Last Post: 07-18-2008, 08:10 AM

Posting Permissions

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