Results 1 to 2 of 2
  1. #1
    Bloitz is offline Member
    Join Date
    May 2011
    Rep Power

    Unhappy Passing objects from classes between JFrames (with video!)

    Hello Java-community,

    I used the search option to go through the forums but didn't quite find what I was looking for (I admit it wasn't an in-depth search because I can't spare the time at the moment).

    Basically I have to make a GUI for a board-game. Since it's a college assignment it's pretty elaborate with a database connection, DomainController, mappers, .. the full coverage so to say. Therefore I can't really copy-paste code for you guys to review because it's, well, a lot.


    (I work with 3 others on this and we're from belgium so some methods and classes will be in Dutch.
    domeinController = domainController in English)

    Youtube video of the GUI so far (I know it looks rubbish but I want it working before I make it pretty :) ): YouTube - GUI layout

    What I wanted to know is how I can pass an object I make from my DomainController between my different JFrames so that I can call non-static methods in the next Jframes from this 1 instance of the DomainController?

    1) Is this possible? if so, should I declare it in my main method in my first JFrame?
    Java Code:
    public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				ChooseLang inst = new ChooseLang();
    				here?==> DomeinController domeinController = new DomeinController();
    2) if this isn't possible:
    2.a) Would I have to pass the instance between JFrames using setters? Like: (this method displays the next window when I click a Jbutton on the first JFrame)

    Java Code:
    public void nextWindow(){
    		chooseChar = new ChooseChar();
    		chooseChar.addWindowListener(new WindowAdapter(){
    			public void windowClosed(WindowEvent close){
                    // add a setter like this here?
    2.b) Would I have to create an instance of DomainController in each JFrame seperately when I need it?
    This is basically what I have done so far but the last JFrame (named ExistingCharacter) makes the user select a character from a list (or create a new one in a different JFrame (named NewCharacter)) and this character should be passed on to the last JFrame (which is the same for both the existingCharacter and newCharacter JFrames). The last JFrame displays the gameboard and puts the character and obstacles on it.

    3) Or would I have to make all my JFrames at once in the main method but keep them setVisible(false) until I need them? poking in the dark here but believe me when I say I tried some weird stuff to get things working over the last couple of days on this project ^^

    I hope I make sense here and it's clear what I'm after. However, if you would like/need more info on the purpose or more code, please feel free to ask and I'll happily oblige.

    Best regards,


    PS: If you have cool ideas for this GUI or other tips/tricks, please do share them. I'm eager to learn :)
    Last edited by Bloitz; 05-09-2011 at 10:01 AM. Reason: extra info + added video

  2. #2
    Bloitz is offline Member
    Join Date
    May 2011
    Rep Power


    I have solved the problem thanks to some extra, in-depth searching.

    I have added an instance of DomainController in the BoardGame constructor and altered the nextScreen() methods in the preceding JFrames:

    This is a nextScreen() method from one of the preceding JFrames:

    Java Code:
    	public void nextScreen(){
    		gameBoard = new GameBoard("GameBoard", this.domainController);
    		gameBoard.addWindowListener(new WindowAdapter(){
    			public void windowClosed(WindowEvent close){
    and altered the GameBoard constructor:

    Java Code:
    public GameBoard(String title, DomainController dC) {
    		this.domeinController = dC;
    This thread opened my eyes:

    A thank you goes to Fubarable for his crystal clear explanation. And of course to all the other java-forums members for the amazing resource of information and knowledge they have gathered.

    /SOLVED :)
    Last edited by Bloitz; 05-09-2011 at 07:25 PM. Reason: modified title to preserve anonymity

Similar Threads

  1. Can't have two JFrames in different classes?
    By grizdog in forum AWT / Swing
    Replies: 9
    Last Post: 12-14-2010, 03:25 AM
  2. Passing objects to classes
    By Catfish1 in forum New To Java
    Replies: 3
    Last Post: 11-30-2010, 05:27 PM
  3. Help: passing data from multiple JFrames
    By newicons in forum New To Java
    Replies: 5
    Last Post: 06-26-2010, 02:55 PM
  4. Passing Objects
    By Java_Developer in forum New To Java
    Replies: 6
    Last Post: 10-29-2009, 01:32 PM
  5. help with passing objects between classes
    By aruna1 in forum New To Java
    Replies: 7
    Last Post: 03-22-2009, 02:41 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