Results 1 to 14 of 14
  1. #1
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default Object orientated problem

    Hi i have two classes.

    The first class extends a JFrame and implements various Listeners, i used it has my Gui.

    The second class wants to be able to get the first classes x and y coords and height and width.
    However when i try to create an object of the Gui class insided of my second class. Its creates another Gui.
    How can i go about getting the Gui.getX() Gui.getY() etc without making another Gui appearing?
    Is there a way inwhich i can just refence an exisiting objects attributes?

    (its important i refence the original Gui object because its location may change, and creating a new Gui object would give me the defualt Gui startup location)
    Last edited by alacn; 08-14-2010 at 07:57 AM.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  2. #2
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    also is it possible to modify a JComponents values from another class?

    i tried doing this code gui.scanProgress.setValue(loopC); from another class an no luck. This problem probably links in with the first post.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by alacn View Post
    Hi i have two classes.

    The first class extends a JFrame and implements various Listeners, i used it has my Gui.

    The second class wants to be able to get the first classes x and y coords and height and width.
    However when i try to create an object of the Gui class insided of my second class. Its creates another Gui.
    How can i go about getting the Gui.getX() Gui.getY() etc without making another Gui appearing?
    Is there a way inwhich i can just refence an exisiting objects attributes?

    (its important i refence the original Gui object because its location may change, and creating a new Gui object would give me the defualt Gui startup location)
    Have a factory that can create (and remember) a Gui object for you. Something like this:

    Java Code:
    public final class GuiFactory {
       private GuiFactory() { } // nobody can create this factory
       private static Gui gui= new Gui(); // the one and only Gui object
    
       public static Gui getGui() { return gui; }
    }
    Ask the Gui factory to pass a refernce to the wanted Gui object and never create one yourself. Also read up on the Singleton pattern.

    kind regards,

    Jos

  4. #4
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Hi, thanks for quick reply

    I made the constructor private but then i was unable to make an object of the GuiFactory class and call the getGui method, so i made it public and created an object to the gulffactory in my 2nd class. then in the 2nd class i did this lines of code

    PHP Code:
        
    App app = new App();
    Gui gui = app.getGui();
    but then i got a nullPointer Error when trying to use gui.

    was i wrong in creating an object of the app class? i didnt know any other way to call the getGui method.
    Last edited by alacn; 08-14-2010 at 09:35 AM.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  5. #5
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    yeah i used System.out and the getGui method is returning null when it should be returning the gui object, any ideas?
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by alacn View Post
    yeah i used System.out and the getGui method is returning null when it should be returning the gui object, any ideas?
    I don't know what you did but I made the GuiFactory() constructor private for a reason: there is no need to create such an object. The getGui() method is static and public so you can simply call it like this:

    Java Code:
    Gui myGui= GuiFactory.getGui();
    The Gui object itself has already been created in the static initialization code of the GuiFactory class.

    kind regards,

    Jos

  7. #7
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    still returns null. code below


    rgb class

    PHP Code:
    Gui guii = App.getGui();
    System.out.println(guii);

    app class


    PHP Code:
    public final class App {
        public static Gui gui = new Gui();
        private App(){}
    
    	public static void main(String[] args) {
    
             
               
               gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	}
            public static Gui getGui() { return gui; }
    
    }
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by alacn View Post
    still returns null. code below


    rgb class

    PHP Code:
    Gui guii = App.getGui();
    System.out.println(guii);

    app class


    PHP Code:
    public final class App {
        public static Gui gui = new Gui();
        private App(){}
    
    	public static void main(String[] args) {
    
             
               
               gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	}
            public static Gui getGui() { return gui; }
    
    }
    I bet you're calling a method from the rgb class from within the constructor of the Gui class ...

    kind regards,

    Jos

  9. #9
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    yeah i got a timer in the gui constructor which calls a method in the rgb class
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by alacn View Post
    yeah i got a timer in the gui constructor which calls a method in the rgb class
    That scenario doesn't work because the Gui object is still being constructed and assignment to that variable in your Factory hasn't been done yet. I suggest a 'two phase' construction:

    1) the constructor creates the bare object
    2) an init() method does all the initialization.

    After 1) has finished the object is assigned in the Factory class and everything in phase 2) can call it in order to obtain the single Gui object.

    kind regards,

    Jos

  11. #11
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    i think my problem is solved, thanks josAH for your time :).
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by alacn View Post
    i think my problem is solved, thanks josAH for your time :).
    Good, you're welcome of course; the following just crossed my mind a couple of minutes ago: you can also move that creation and singleton business to the Gui class itself where the constructor can assign itself to the private static variable; like this:

    Java Code:
    public class Gui {
       private static Gui gui;
       // create a single Gui object:
       static {
          new Gui();
       }
       public static Gui getGui() { return gui; }
       ...
       private Gui() {
          Gui.gui= this; // assign yourself before doing anything else ...
          ...
       }
    }
    kind regards,

    Jos

  13. #13
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    some crazy looking syntax tbh :eek:
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by alacn View Post
    some crazy looking syntax tbh :eek:
    There's nothing special about it: there's the private static gui variable and a public method to hand it over to something else. The Gui constructor assigns itself to that variiable before anything else happens. The static { } block is just a static initializer and executes just after the class has been loaded.

    This scenario also avoids that ugly two phase construction because the gui variable is assigned a Gui object before anything else happens.

    kind regards,

    Jos

Similar Threads

  1. Problem with class Object
    By marak in forum New To Java
    Replies: 9
    Last Post: 07-24-2010, 03:40 PM
  2. Problem in String object
    By Yuvarajsinh in forum New To Java
    Replies: 6
    Last Post: 04-12-2010, 11:59 AM
  3. Problem in using Object datatype
    By mfaizan24 in forum New To Java
    Replies: 6
    Last Post: 05-05-2009, 11:51 PM
  4. Problem with Date Object
    By R O C K Y in forum Advanced Java
    Replies: 4
    Last Post: 02-15-2009, 04:37 PM
  5. Replies: 2
    Last Post: 12-10-2008, 04:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •