Hi guys.
I have a question. Let us say that I write a MainForm class which has a
jDesktopPane, MenuBar and ToolBar. I would like to write classes for each
jInternalFrame and call it from MainForm using menu event listeners. But
when I try to access jDesktopPane from this outside class to add a
jInternaFrame, it says something about non static variable could not be
refernced from static ...
How do you update a component in another class?
Thanks
Zvonko
Andrew Thompson - 10 Aug 2006 07:33 GMT
..
> I have a question. Let us say that I write a MainForm class which has a
> jDesktopPane, MenuBar and ToolBar. I would like to write classes for each
[quoted text clipped - 4 lines]
>
> How do you update a component in another class?
Something like make it static..
And a couple of notes:
- a good group for Java begiiners as comp.lang.java.help
- once you figure how to ask questions, and become a little
more confident with the language, comp.lang.java.gui is
a good place to discuss GUI related matters.
- please type (or better, copy) error messages and stacktraces
*exactly* so that people can give you more specific help.
- I suspect your IDE (NetBeans?) is leading you into deep waters,
best put it aside for the moment and get a grip on the basics of
Java (you should probably not be coding GUI apps. yet).
Andrew T.
Ian Wilson - 11 Aug 2006 12:09 GMT
> Hi guys.
>
[quoted text clipped - 4 lines]
> jInternaFrame, it says something about non static variable could not be
> refernced from static ...
When learning Java I encountered this kind of error quite a lot. This
was probably because I was previously used to procedural languages more
than object-oriented languages. This led me to often create classes as
mere containers for static methods.
I found that these sort of problems were greatly reduced when I made a
serious effort to refactor my code so that I was instantiating objects
and then calling instance methods.
> How do you update a component in another class?
I still consider myself a learner of Java so the following may be
completely off base, if so maybe someone more expert will jump in and
we'll both learn.
I'll assume you have something like:
class MainForm extends JFrame ...
...
class FooInternalFrame extends JInternalFrame ...
...
class BarInternalFrame extends JInternalFrame ...
Where possible, I try to avoid getting one class to directly update a
component in another class. I think it useful to have as clean a
separation as possible between classes.
Your MainForm could pass, to the JInternalFrame constructor, a reference
to the MainForm. The JInternalFrame could use this to invoke a method of
the MainForm. I've done this sort of thing but it doesn't feel quite right.
I've found defining and implementing Interfaces a useful way to control
this type of behaviour.
I wonder if it may be more Javaish for the MainForm to register a
"listener" in the JInternalFrame.
class MainForm extends JFrame implements XyzListener ...
public void XyzHappened() {
// update my component
}
...
FooInternalFrame jif = new FooInternalFrame();
jif.addXyzListener(this);
...
class FooInternalFrame extends JInternalFrame ...
public void addXyzListener(XyzListener baz) {
// add baz to list of listeners
}
...
// Something happened that needs
// MainFrame to update a component.
// Iterate over listeners
aListener.XyzHappened();