[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Inter object communication in Java?

Posted on 2011-10-17
11
Medium Priority
?
384 Views
Last Modified: 2012-05-12
I have a code which is half generated by Netbeans IDE through selecting the containers and controllers.  Within the project I have a couple of packages which should work together and be able to see one another's variables and methods.  In one of the packages I have a JList item which needs to be populated when the JPanel from this other package receives the user input.  However for some reason this link is not possible!  

I even place a public getter method inside the LMBView class but still can't access the LMBView object!


PACKAGE LMB ------------------------------------------------

//This is the main class initializing the LMBView class
public class IDAApp extends SingleFrameApplication {

      @Override
       protected void startup() {
            show(new LMBView(this));
        }
}

    
public class LMBView extends FrameView {

      public javax.swing.JList jList_PortalList;

      private void initComponents() {
             jList_PortalList = new javax.swing.JList();
                  .
                  .
                  .
      }

Open in new window


PACKAGE LMB.resources---------------------------------------

LMB.LMBView.jList_PortalList  // DOES NOT WORK OR LINKS TO THE CALLING OBJECT

Open in new window




This is the error message shown

     
non-static variables jList_PortalList cannot be referenced from a static context

Open in new window

0
Comment
Question by:snajalm
  • 6
  • 3
  • 2
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36980104
>>public javax.swing.JList jList_PortalList;

Don't do that. Make it private and do the below, calling that method from where you want to access the JList
public JList getPortalList();
    return jList_PortalList;
}

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 36980130
If you do

>>show(new LMBView(this));

you're going to lost the reference to LMBView, so you won't be able to call the method i just gave you. You need
LMBView lmb = new LMBView(this);
JList portalList = lmb.getPortalList();

Open in new window

0
 

Author Comment

by:snajalm
ID: 36980302
Is this the right way of constructing it??!  This LMBApp class contains the main view which ultimately make a call to the LMBView class.  I applied your fix but it seems not working!

/*
 * LMBApp.java
 */
package ida;

import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class LMBApp extends SingleFrameApplication {
    LMBView lmbView;
    /**
     * At startup create and show the main frame of the application.
     */
    @Override
    protected void startup() {
        lmbView = new LMBView(this);
        show(lmbView);
        //show(new IDAView(this));
    }
    
    public LMBView getLMBViewInstance() {
        return lmbView;
    }
    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override
    protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of IDAApp
     */
    public static IDAApp getApplication() {
        return Application.getInstance(LMBApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) throws Exception {
        launch(LMBApp.class, args);
    }
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 36980335
What about it is 'not working'?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36980366


non-static variables jList_PortalList cannot be referenced from a static context

this means you are using your non-static instance  variable jList_PortalList
within a static mathod. But in the code which you show we don't see any static
methods. Which is the line which generates thios error?
0
 

Author Comment

by:snajalm
ID: 36980448
Here's what I've done which is working but I'd want to know whether this is logically right!!!!  

Please look at the methods getLMBViewInstance() and startup()  (as CEHJ) recommended it!  Now it seems that I can have a little getter method inside the LMBView class and access the jList_PortalList controller!

/*
 * LMBApp.java
 */
package lmb;

import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class LMBApp extends SingleFrameApplication {
    static LMBView lmbView;
    /**
     * At startup create and show the main frame of the application.
     */
    @Override
    protected void startup() {
        lmbView = new LMBView(this);
        show(lmbView);
        //show(new LMBView(this));
    }
    
	// IS THIS METHOD LOGICALLY RIGHT
    public static LMBView getLMBViewInstance() {
        return lmbView;
    }
    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override
    protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of LMBApp
     */
    public static LMBApp getApplication() {
        return Application.getInstance(LMBApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) throws Exception {
        launch(LMBApp.class, args);
    }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36980496
Seems not worse than anything else.

If I were you, I'd go away form all this NetBeans stuff.
It is all so confusing.

And it is much better to create these GUI yourself - than you are in command of everything
and don't depend on their weird code.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36980716
Yes. What i'm not familiar with is the JSR on which the framework is based, so i can't tell you if it's 'correct' from a design point of view. From a programming point of view, it's fine
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 2000 total points
ID: 36980730
You'll need to study it probably:

http://jcp.org/en/jsr/detail?id=296
0
 

Author Closing Comment

by:snajalm
ID: 37060531
Thank you!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37065335
:)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question