Results 1 to 14 of 14
Thread: Object orientated problem
- 08-14-2010, 07:55 AM #1
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
- 08-14-2010, 08:11 AM #2
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
- 08-14-2010, 08:50 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Have a factory that can create (and remember) a Gui object for you. Something like this:
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.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; } }
kind regards,
Jos
- 08-14-2010, 09:32 AM #4
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
but then i got a nullPointer Error when trying to use gui.PHP Code:App app = new App(); Gui gui = app.getGui();
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
- 08-14-2010, 09:49 AM #5
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
- 08-14-2010, 10:40 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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:
The Gui object itself has already been created in the static initialization code of the GuiFactory class.Java Code:Gui myGui= GuiFactory.getGui();
kind regards,
Jos
- 08-14-2010, 11:28 AM #7
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
- 08-14-2010, 12:11 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 08-14-2010, 12:26 PM #9
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
- 08-14-2010, 12:40 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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
- 08-14-2010, 12:51 PM #11
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
- 08-14-2010, 01:03 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,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 ... ... } }
Jos
- 08-14-2010, 01:25 PM #13
some crazy looking syntax tbh :eek:
Teaching myself java so that i can eventually join the industry! Started in June 2010
- 08-14-2010, 01:30 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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
-
Problem with class Object
By marak in forum New To JavaReplies: 9Last Post: 07-24-2010, 03:40 PM -
Problem in String object
By Yuvarajsinh in forum New To JavaReplies: 6Last Post: 04-12-2010, 11:59 AM -
Problem in using Object datatype
By mfaizan24 in forum New To JavaReplies: 6Last Post: 05-05-2009, 11:51 PM -
Problem with Date Object
By R O C K Y in forum Advanced JavaReplies: 4Last Post: 02-15-2009, 04:37 PM -
Stuck with Java Object Orientated Programming - Arrays
By Phalanx in forum New To JavaReplies: 2Last Post: 12-10-2008, 04:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks