Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By rru96

Thread: Passing JFrame to multiple classes

  1. #1
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    97
    Rep Power
    0

    Default Passing JFrame to multiple classes

    Below is code for the start of a little program. I am getting confused on how to pass this JFrame around so that multiple classes are able to share it. Line 23 points to a new Prog class. In the constructor of that class, I call a Splash Class that is supposed to display a splash screen on this main JFrame. Instead, I am getting a grey box. I know my images are loading, I have been able to get the Splash working, but not by being called from the Prog class. If I call Splash from my Start class, it works but I can't get Prog working. If this makes sense, I would greatly appreciate a little nudge as to what I am doing wrong.

    Java Code:
    package com.ryan;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    
    public class Start {
    
    	public JFrame f;
    	
    	public Start(){
    		EventQueue.invokeLater(new Runnable(){
    			public void run(){
    				f = new JFrame();
    				f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				f.setMinimumSize(new Dimension(800,750));
    				f.setResizable(false);
    				f.setLocationRelativeTo(null);
    				f.setUndecorated(true);
    				f.setVisible(true);
    				Prog p = new Prog();
    				f.add(p);
    				f.pack();
    				
    				
    				
    			}
    		});
    	}
    	public static void main(String[] args){
    		new Start();
    	}
    }
    BenMoon likes this.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Passing JFrame to multiple classes

    Can't you just pass your f variable into the Prog constructor?

    Why do you need to pass the JFrame at all? What does Prog do?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    97
    Rep Power
    0

    Default Re: Passing JFrame to multiple classes

    Prog will load my JPanels. Splash shows a simple image that covers the frame. My thought process was

    Start() calls new Splash() which finishes and calls new Prog();
    But the confusion was having Prog() see the JFrame.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Passing JFrame to multiple classes

    In that case, can you pass the JFrame into the Prog constructor?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    97
    Rep Power
    0

    Default Re: Passing JFrame to multiple classes

    I can, but I guess I am still lost on passing around parameters. I just don't get how to make all the classes keep looking at the original JFrame. So what I did for now was
    Java Code:
    package com.ryan;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    
    public class Start {
    
    	public JFrame f;
    	
    	public Start(){
    		EventQueue.invokeLater(new Runnable(){
    			public void run(){
    				f = new JFrame();
    				f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				f.setMinimumSize(new Dimension(800,700));
    				f.setResizable(false);
    				f.setLocationRelativeTo(null);
    				f.setUndecorated(true);
    				f.setVisible(true);
    				Splash s = new Splash(f);
    				f.add(s);
    				f.pack();
    				Prog p = new Prog(f);
    				p.showControlPanel();
    				f.add(p);
    				f.pack();
    				
    				
    				
    				
    			}
    		});
    	}
    	public static void main(String[] args){
    		new Start();
    				
    	}
    }
    Its working, though doesnt seem to be efficient

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Passing JFrame to multiple classes

    To do what you want just pass the JFrame instance in a constructor for other classes. Then those classes have a reference to that JFrame and can use it however is necessary.

    Java Code:
    JFrame frame = new JFrame();
    Foo foo = new Foo(frame,...);
    Bar bar = new Bar(frame,...);
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Passing JFrame to multiple classes

    Quote Originally Posted by rru96 View Post
    Its working, though doesnt seem to be efficient
    What is inefficient about it? What do the other classes actually do with the JFrame?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    97
    Rep Power
    0

    Default Re: Passing JFrame to multiple classes

    Well, not inefficient, but still just not understanding why Splash() isn't getting called in its entirety and then coming back and running Prog(). Instead, they are both running and I dont get why.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Passing JFrame to multiple classes

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Passing JFrame to multiple classes

    We don't know what Splash or Prog are doing, so it's a little hard to comment on what might be going on. If you want more specific help, I suggest posting an SSCCE (eliminate all the extra stuff not directly related to the problem) and we can go from there.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    97
    Rep Power
    0

    Default Re: Passing JFrame to multiple classes

    I think you all have actually helped me out. I have watched a few more videos and read a little more. THank you all. I am trying to do to much and I need to slow down with more code Java as this little project requires some new thing to learn. Thank you again guys.

    DB- I found out about that after I built my SplashScreen. I decided to keep what I had for now though want to implement a true SplashScreen. Thank you

Similar Threads

  1. Passing JLabels between classes
    By nat45928 in forum New To Java
    Replies: 1
    Last Post: 05-10-2011, 02:59 AM
  2. Passing variables through classes?
    By EternalFacepalm in forum New To Java
    Replies: 4
    Last Post: 04-27-2011, 12:46 AM
  3. Replies: 6
    Last Post: 04-12-2010, 11:33 AM
  4. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  5. Need help passing data between classes
    By bri1547 in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 04:19 AM

Posting Permissions

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