Results 1 to 14 of 14
  1. #1
    hvince95 is offline Member
    Join Date
    Jan 2012
    Location
    Adelaide, Australia
    Posts
    17
    Rep Power
    0

    Default Working with multiple JComponents

    Hello, I have been making a game and it works great! I have now been trying to implement a menu by putting the main game in a JComponent, and the menu in a seperate JComponent. In a main class I add them both to the JFrame and do a little test to make only the enabled one visible. It doesnt really work. Please help! Oasis is one JComponent, MainMenu is another (both extend JComponent).
    Java Code:
    public class Main extends JFrame implements GameConfig{
    	
    	private Oasis gamePanel = new Oasis();
    	private MainMenu menuPanel = new MainMenu();
    	private String gamePhase = "runGame";
    	
    	public Main() {
    
    		this.setTitle("Oasis");
    		this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    		this.setLocationRelativeTo(null);
    		this.setResizable(false);
    		this.setVisible(true);
    		
    		addWindowListener(new WindowAdapter() {
    			@Override
    			public void windowClosing(WindowEvent e) {
    				setVisible(false); // hide the window quickly
    				dispose(); // release all system resources
    				System.exit(0); // finally exit.
    			}
    		});
    		
    		add(gamePanel);
    		add(menuPanel);
    		if (gamePhase.equals("runGame")) {
    			menuPanel.setVisible(false);
    			menuPanel.setEnabled(false);
    			gamePanel.setVisible(true);
    			gamePanel.setEnabled(true);
    		}
    		if (gamePhase.equals("mainMenu")) {
    			gamePanel.setVisible(false);
    			gamePanel.setEnabled(false);
    			menuPanel.setVisible(true);
    			menuPanel.setEnabled(true);
    		}
    	}
    	
    	public static void main(String[] args) {
    		@SuppressWarnings("unused")
    		Main gameFrame = new Main();
    	}
    }
    But it only shows a white/ish screen and not the game component. (It should definately draw something because if I only add the game component it works perfectly, but without a menu!)
    Thankyou very much.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Working with multiple JComponents

    Don't try to fiddle with this yourself when the CardLayout was made just for this situation. So have the contentPane use this layout and swap views when necessary.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Working with multiple JComponents

    Java Code:
    add(gamePanel);
    add(menuPanel);
    You add these panels to the frame's content pane which has a border layout. The effect is that menuPanel will replace gamePanel since both can't occupy the center of the content pane.

    You could set a different layout manager on the frame's content pane. Or, better I think, create a panel to hold the gamePanel and menuPanel and give this other panel an appropriate layout manager (CardLayout?). Then set this other panel as the content pane of your frame.

    See Using toplevel Containers and Laying out Components within a Container in Oracle's Tutorial.

    -----

    This is a Swing question do I'll move it to the Awt/Swing forum.

  4. #4
    hvince95 is offline Member
    Join Date
    Jan 2012
    Location
    Adelaide, Australia
    Posts
    17
    Rep Power
    0

    Default Re: Working with multiple JComponents

    Thankyou for your quick answers and moving the thread, I'll have to check out how to use CardLayout!

  5. #5
    hvince95 is offline Member
    Join Date
    Jan 2012
    Location
    Adelaide, Australia
    Posts
    17
    Rep Power
    0

    Default Re: Working with multiple JComponents

    so that works like a treat (Thankyou so much!)
    But there is one problem, I cannot access the CardLayout JPanel object from within the two components that it is holding to be able to change which one is being shown.
    If that is a bit confusing, in the Main class, I create the JPanel with cardlayout layout. I create two custom JComponent classes (extends jcomponent), and it is within them that I need to be able to access the JPanel within the main class.
    How do you think I should do this?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Working with multiple JComponents

    You could pass a reference from one class into the other. For instance, you could give the top level GUI a public class that allows other classes to swap views, say something like

    Java Code:
    public void swapView(String name) {
    
        // cardLayout is a class field that refers to the CardLayout
        // cardContainer is a class field that refers to the container that uses CardLayout
        cardLayout.show(cardContainer, name);
    
    }
    You could also give this class public static void String constants that hold the name Strings.

    Then pass a reference of the top level gui class to the subclasses so that they can call this method when necessary.

  7. #7
    hvince95 is offline Member
    Join Date
    Jan 2012
    Location
    Adelaide, Australia
    Posts
    17
    Rep Power
    0

    Default Re: Working with multiple JComponents

    Quote Originally Posted by Fubarable View Post
    You could pass a reference from one class into the other. For instance, you could give the top level GUI a public class that allows other classes to swap views
    What do you mean by this? put another class within the main class? I dont understand. please help!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Working with multiple JComponents

    Quote Originally Posted by hvince95 View Post
    What do you mean by this? put another class within the main class? I dont understand. please help!
    You're already doing this as your main class will of necessity hold other objects, Oasis and MainMenu. If you want to allow objects of these classes to call methods of the main object, you'll need to pass a reference from the main into them, likely through a constructor parameter:


    Java Code:
    	
    public class Main extends JFrame implements GameConfig{
         
        private Oasis gamePanel = new Oasis(this);
        private MainMenu menuPanel = new MainMenu(this);
        private String gamePhase = "runGame";
    Java Code:
    public class Oasis {
       private Main main;
    
       public Oasis (Main main) {
          this.main = main;
          
          // ... plus other code for the constructor.
       }
    
       // then in an ActionListener for some JButton or menu, you'll call 
       // main.swapView(Main.OTHER_PANEL);
    
    }

  9. #9
    hvince95 is offline Member
    Join Date
    Jan 2012
    Location
    Adelaide, Australia
    Posts
    17
    Rep Power
    0

    Default Re: Working with multiple JComponents

    Wow, I was never aware you could pass the whole class itself as a parameter. Thankyou so much!!! I appreaciate your help.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Working with multiple JComponents

    You're welcome! Did it work?

  11. #11
    hvince95 is offline Member
    Join Date
    Jan 2012
    Location
    Adelaide, Australia
    Posts
    17
    Rep Power
    0

    Default Re: Working with multiple JComponents

    haha, have not tried it yet, no time!
    I will definitely post back here to let you know though.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Working with multiple JComponents

    OK, please do!

  13. #13
    hvince95 is offline Member
    Join Date
    Jan 2012
    Location
    Adelaide, Australia
    Posts
    17
    Rep Power
    0

    Default Re: Working with multiple JComponents

    Worked like a treat, thankyou so much!

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 3
    Last Post: 08-29-2011, 01:54 AM
  2. HashMaps containing JComponents
    By chillin in forum AWT / Swing
    Replies: 3
    Last Post: 05-04-2011, 12:05 AM
  3. JComponents Are Wrong Size
    By MrFish in forum AWT / Swing
    Replies: 2
    Last Post: 12-21-2010, 03:13 AM
  4. Multiple JPanels Not Working
    By jgezau in forum AWT / Swing
    Replies: 5
    Last Post: 04-17-2009, 04:52 AM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 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
  •