Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Newbie question

Thread view: 
Zvonko - 10 Aug 2006 07:04 GMT
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();


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.