Results 1 to 8 of 8

Thread: referencing problem

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    Sydney, Australia
    Posts
    37

    referencing problem

    I'm not sure what I'm on, but I can't seem to get this right. I'm trying to reference a frame declared in one class from another class which is used exclusively to handle events.

    In the first:
    VB Code:
    1. import java.awt.*;
    2.  
    3. public class test {
    4.  
    5.     public Frame frmMain = new Frame("Test");
    6.  
    7.     public static void main(String args[]) {
    8.         test objTest = new test();
    9.     }
    10.  
    11.     public test() {
    12.         frmMain.addMouseListener(new mouseEvents());
    13.  
    14.         frmMain.setSize(100,100);
    15.         frmMain.setLocation(400,400);
    16.         frmMain.setVisible(true);
    17.     }
    18. }


    In the second:
    VB Code:
    1. import java.awt.event.*;
    2.  
    3. public class mouseEvents extends MouseAdapter {
    4.     public void mousePressed(MouseEvent e) {   
    5.         System.out.println(test.frmMain.getTitle());
    6.     }
    7. }

    This is similar to what I'm trying to do. I get the "non-static variable frmMain cannot be referenced..." error on the println line.

    Sunny

    * No trees were harmed in the making or sending of this message. However a great number of electrons were terribly inconvenienced.

  2. #2
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228

    Thumbs up

    Off the top of my head, you might need to do something like this:
    Code:
    public void mousePressed(MouseEvent e) {
     Object o = e.getSource();
     if(o instanceof Frame)
      System.out.println(((Frame)o).getTitle());
    //And I'm also thinking of getParent until you get the class you were looking for; then a test on the title or other parameter would tell you if it was the right object since you'd already have the right class
    }

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Or make frmMain static.
    public static Frame frmMain = new Frame("Test");
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    Sydney, Australia
    Posts
    37
    Thanks for the ideas so far.

    As much as I'd like to make it static...it's really an easy way out and its making other problems in other parts of the program (I really hate the static/non-static thing Java has).

    Is there a more efficient way then Phenix's idea? Because if the event source was a button, that was in a panel in a frame. There might be a fair few getParents before I get to the right object.

    Surely there is a simple and efficient way - VB does!
    Sunny

    * No trees were harmed in the making or sending of this message. However a great number of electrons were terribly inconvenienced.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    VB has one? Which?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Lively Member
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    115
    When I use listeners, I always use anonymous inner-classes (if you can call them that ). In your case it would look like:

    Code:
    frmMain.addMouseListener(new MouseAdapter()
    {
    	public void mousePressed(MouseEvent e) {
                      System.out.println(frmMain.getTitle());
    	}
    });
    I haven't compiled it and tested it, but the principle is correct.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can call them that and they are called that.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    Sydney, Australia
    Posts
    37
    I got an idea from a friend, and thought I'd share it here.

    A reference to the original object is passed as a parameter to the constructor of the second class (the event handler). That way the event handler can call back to the original object.

    In the first class:
    VB Code:
    1. import java.awt.*;
    2. import java.awt.event.*;
    3.  
    4. public class test {
    5.  
    6.     private Frame frmMain = new Frame("test");
    7.     private TextArea txtMain = new TextArea("");
    8.  
    9.     private Button b1 = new Button("Dialog");
    10.    
    11.     public static void main (String args[]) {
    12.         test objTest = new test();
    13.     }
    14.  
    15.     public test() {
    16.         frmMain.setSize(400,400);
    17.         frmMain.setLayout(new BorderLayout()); 
    18.  
    19.         // pass this as parameter  
    20.         b1.addActionListener(new buttonEvents(this));
    21.  
    22.         frmMain.add(b1, BorderLayout.SOUTH);
    23.         frmMain.setVisible(true);
    24.     }
    25. }

    In the event handler:
    VB Code:
    1. import java.awt.*;
    2. import java.awt.event.*;
    3.  
    4. public class buttonEvents implements ActionListener {
    5.    
    6.     test objTest = null;
    7.  
    8.     // use the constructor to get a reference to the main object
    9.     public buttonEvents(test objSource) {
    10.         this.objTest = objSource;
    11.     }
    12.  
    13.     public void actionPerformed(ActionEvent e) {
    14.         // some code in here for the event, and can use objTest to refer back to the original object
    15.     }
    16. }
    Sunny

    * No trees were harmed in the making or sending of this message. However a great number of electrons were terribly inconvenienced.

Posting Permissions

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



Click Here to Expand Forum to Full Width