Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java.Observable

  • 23-04-2009 03:10PM
    #1
    Registered Users, Registered Users 2 Posts: 1,119 ✭✭✭


    I have a JFrame observing another class. And this works fine when I launch the JFrame normally. However when I create the JFrame from within another JFrame, the Observing JFrame does not observe. It just stays with its default values for everything.

    I'm adding the observer using object.addObserver(this);


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 10,093 Mod ✭✭✭✭marco_polo


    It is probably not working because JFrame is a Top Level component and it does not make sense to create one inside another, and will probably result in all kinds of unexpected behaviour. If you want a window within a window in your application use a JDialog or an Internal Frame instead.

    See here for more details

    http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html


  • Registered Users, Registered Users 2 Posts: 1,119 ✭✭✭Donald-Duck


    Sorry I meant opening a new JFrame, in a new window, from a button in original one.


  • Moderators, Science, Health & Environment Moderators Posts: 10,093 Mod ✭✭✭✭marco_polo


    Could you post up a little more code perhaps? Don't have much to go on at the moment. Where is object.addObserver(this); called from for example?


  • Registered Users, Registered Users 2 Posts: 1,119 ✭✭✭Donald-Duck


    public class GameWindow extends JFrame implements java.util.Observer
    {
    	public GameWindow() {
            	...
                	aTable.addObserver(this);
                	aTable.initialise();           
    	}
    }
    
    public class Table extends java.util.Observable {
    	
    	public Table() {
    		...
    	}
    	...
    }
    
    class LoginWindow {
    	private void singlePlayerClicked()
    	{
    			GameWindow g = new GameWindow();
    	}
    }
    

    If the code is left to wait for user interaction in the GameWindow JFrame it just freezes as a white window, with the observed Table running correctly.
    If I remove the user interaction, the GameWindow runs fine, JFrame opens correctly however it doesn't observe the Table correctly and none of the values are ever updated in the JFrame.

    I think I've included what I think are the important parts of the code, I didn't originally write most of it as it was done by another guy in the project group.


  • Registered Users, Registered Users 2 Posts: 885 ✭✭✭clearz


    In your aTable.initialise(); method try adding

    setChanged();
    notifyObservers();

    at the end. Not sure if this is where you want the callback to happen but.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,119 ✭✭✭Donald-Duck


    Unfortuantely thats in it already.

    If I was to launch GameWindow from main it all works perfectly.


  • Registered Users, Registered Users 2 Posts: 885 ✭✭✭clearz


    Unfortuantely thats in it already.

    If I was to launch GameWindow from main it all works perfectly.


    Well then as marco_polo has said can you show us all code releated to the observer pattern


  • Registered Users, Registered Users 2 Posts: 1,119 ✭✭✭Donald-Duck


    I'll get the guy who wrote it to write a smaller project that causes the same bug, couple of thousand lines involved so it'd be pretty tedious for anyone here to look through.


  • Moderators, Science, Health & Environment Moderators Posts: 10,093 Mod ✭✭✭✭marco_polo


    I got the brain going and I think I have an answer. I suspect that it is a threading issue.

    The JVM uses a single thread per JVM to handle all GUI events called the event dispatch thread. As you are creating the second JFrame within this event handling thread (when the user clicks a button) it is likely the cause of your problem.

    If you kick off a new thread for the second JFrame I think it should work ok.

    Something like this should do
    private void singlePlayerClicked(){
    
        Runnable r = new Runnable() {
              public void run() {
                  new GameWindow();
              }
        });
    Thread.start (r)
    
    
    	
    
    
    
    

    ** EDIT oh wait when you remove the user interaction it still doesn't observe.
    Only half way there so :o


Advertisement