0

I've got a main JFrame that creates an instance of a basic notifications JFrame class.

My code for creating the notifications from my main JFrame looks like this:

new Notification(from, msg, time);

I am wondering how i from within my notifications class can access my main JFrame. Basically i want to change setVisible for some components on the main JFrame from within my notifications class.

EDIT

My client.java (main JFrame) calls the notification

public JPanel pnlMidMenuButtons;
/**** code... **/
Notification ntf = new Notification(from, msg, time); // Further down the notification is being called

ImportUI:

public class ImportUI extends Client implements NotificationParent {

public void setImportantFieldsVisible(boolean visible) {
    pnlMidMenuButtons.setVisible(visible);
}

}

NotificationParent:

public interface NotificationParent {
    public void setImportantFieldsVisible(boolean visible);
    public void setAgentName(String agentName);
}

And my notification class:

public class Notification extends JFrame {

    private NotificationParent parent;
    /*...*/

    public Notification(NotificationParent parent, String from, String msg, Date time) {
        this.parent = parent;
        parent.setImportantFieldsVisible(false); // Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    }

}

Any ideas whats causing the exception?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Alosyius
  • 8,771
  • 26
  • 76
  • 120
  • I would strongly discourage you from allowing any class outside of you frame enough access to it's contents. Instead, I might consider using some kind of `interface` that you could implement that exposes the functionality that needs to be achieved. This prevents the `Notification` class from going postal and doing things to the main window it shouldn't. Then, you would simply provide a parameter of the same type as the interface and pass a reference of the implementation to it... – MadProgrammer Sep 16 '13 at 07:07
  • @MadProgrammer sounds like a really good solution. How would one implement an interface like that? Not quite sure i follow the logic – Alosyius Sep 16 '13 at 07:20
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Sep 16 '13 at 08:18

1 Answers1

1

The actual answer will depend on your needs, but I would create an interface (or even a series of interfaces if you wanted to provide some functionality to some objects and not others).

public interface NotificationParent {
    public void setImportantFieldsVisible(boolean visible);
}

I would then implement this interface on the caller...

public class ImportUI extends ... implements NotificationParent {
    /*...*/
    public void setImportantFieldsVisible(boolean visible) {
        //....
    }
}

I would then from an additional parameter to the Notification so it could accept a reference of NotificationParent...

public class Notification extends ... {

    private NotificationParent parent;
    /*...*/

    public Notification(NotificationParent parent, String from, String msg, Date time) {
        this.parent = parent;
        /*...*/
    }

}

Then, we ever you needed to, you would just call one of the available methods to fulfill your requirements.

Now you could, obviously, have more methods, but that comes down to your requirements...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • What should be `Notification` be extending? – Alosyius Sep 16 '13 at 07:34
  • Whatever you need to it to extend...it's an example ;) – MadProgrammer Sep 16 '13 at 07:37
  • Ok get it :) And the `public class ImportUI extends ...` should extend my class that is calling the Notification class? – Alosyius Sep 16 '13 at 07:38
  • Starting to get it; and when i want to call methods from the interface id go like; `parent.setImportantFieldsVisible(false);` ? – Alosyius Sep 16 '13 at 07:44
  • Yep. Personally I would set up "group" or "batch" methods that allow you to effect the state of a number of fields from within the method, or set up some kind of state flag which allows the parent to decide the best response to take to the given state, but that's up to you ;) – MadProgrammer Sep 16 '13 at 07:45
  • Sounds good! For some reason i get `Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException` when running `parent.setImportantFieldsVisible(true);` any ideas what that could be ? – Alosyius Sep 16 '13 at 07:46
  • Two things. 1- The reference you passed is null; 2- You didn't assign it to an instance variable when it was passed to your class. – MadProgrammer Sep 16 '13 at 07:49
  • Posted my code above cant seem to figure out why i get the exception – Alosyius Sep 16 '13 at 07:58
  • Your formal and actual parameters don't match. You're saying your creating a `Notification` with `Notification ntf = new Notification(from, msg, time)`, but the constructor says it needs `Notification(NotificationParent parent, String from, String msg, Date time)`...You don't have a second constrcutor do you ? – MadProgrammer Sep 16 '13 at 08:00
  • Oh so instead of `Notification ntf = new Notification(from, msg, time)` what would i write? `Notification ntf = new Notification(this, from, msg, time)? ` – Alosyius Sep 16 '13 at 08:03
  • That would seem reasonable, otherwise you won't have a reference to `NotificationParent` with which to play with... – MadProgrammer Sep 16 '13 at 08:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37447/discussion-between-alosyius-and-madprogrammer) – Alosyius Sep 16 '13 at 08:10