|
-
Jan 7th, 2003, 09:11 AM
#1
Thread Starter
Member
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:
import java.awt.*;
public class test {
public Frame frmMain = new Frame("Test");
public static void main(String args[]) {
test objTest = new test();
}
public test() {
frmMain.addMouseListener(new mouseEvents());
frmMain.setSize(100,100);
frmMain.setLocation(400,400);
frmMain.setVisible(true);
}
}
In the second:
VB Code:
import java.awt.event.*;
public class mouseEvents extends MouseAdapter {
public void mousePressed(MouseEvent e) {
System.out.println(test.frmMain.getTitle());
}
}
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.
-
Jan 7th, 2003, 01:48 PM
#2
Addicted Member
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
}
-
Jan 7th, 2003, 04:21 PM
#3
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.
-
Jan 10th, 2003, 02:05 AM
#4
Thread Starter
Member
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.
-
Jan 11th, 2003, 11:52 AM
#5
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.
-
Jan 11th, 2003, 01:24 PM
#6
Lively Member
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.
-
Jan 11th, 2003, 04:55 PM
#7
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.
-
Jan 12th, 2003, 04:29 AM
#8
Thread Starter
Member
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:
import java.awt.*;
import java.awt.event.*;
public class test {
private Frame frmMain = new Frame("test");
private TextArea txtMain = new TextArea("");
private Button b1 = new Button("Dialog");
public static void main (String args[]) {
test objTest = new test();
}
public test() {
frmMain.setSize(400,400);
frmMain.setLayout(new BorderLayout());
// pass this as parameter
b1.addActionListener(new buttonEvents(this));
frmMain.add(b1, BorderLayout.SOUTH);
frmMain.setVisible(true);
}
}
In the event handler:
VB Code:
import java.awt.*;
import java.awt.event.*;
public class buttonEvents implements ActionListener {
test objTest = null;
// use the constructor to get a reference to the main object
public buttonEvents(test objSource) {
this.objTest = objSource;
}
public void actionPerformed(ActionEvent e) {
// some code in here for the event, and can use objTest to refer back to the original object
}
}
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|