Results 1 to 3 of 3
  1. #1
    Aldane is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default JPanel add/remove in a JFrame

    Whenever It try to switch panels by adding a new one and removing the old one (like on the bold code below), the program (or its display) freezes. Is there any code that I need to add/change? Please help. Many Thanks :D

    Java Code:
    public class Game 
    {
    	JFrame myFrame = new JFrame ();
    	
    	Board back = new Board();
    	CharacterManager charac = new CharacterManager();
    	TitleScreen TS = new TitleScreen();
    	
    	int dispState = 0;
    
    	public Game()
    	{
    		back.setOpaque(false);
    		charac.setOpaque(false);
    		
    		myFrame.setSize(600,700);
    		myFrame.setResizable(false);
    		myFrame.setVisible(true);
    		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		myFrame.getContentPane().add(TS);
    		
    		myFrame.addMouseListener(new Events());
    		myFrame.addMouseMotionListener(new Events());		
    	}
    	
    	// Mouse Events Class
    	public class Events implements MouseListener, MouseMotionListener, KeyListener
    	{
    		public void mouseMoved(MouseEvent e) 
    		{	
    			...
    		}
    		
    		public void mouseClicked(MouseEvent e) 
    		{
    			//Title Screen
    			if(dispState == 0)
    			{
    				if(TS.mouseAtStart)
    				{
    					dispState = 1;
    				        [B]myFrame.getContentPane().remove(TS);
    					myFrame.getContentPane().add(back);[/B]
    				}
    			
    				else if(TS.mouseAtHelp)
    				{
    					
    				}
    				
    				else if(TS.mouseAtScores)
    				{
    					
    				}
    			
    				else if(TS.mouseAtExit)
    				{
    					System.exit(0);
    				}
    			}
    		
    			//Board
    			else if(dispState == 1)
    			{
    		
    			}
    		}
                  ....
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    What do you mean it freezes? Are there exceptions thrown? If you dynamically add components to a realized Container (in this case a visible JFrame) call revalidate

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

    Default

    Quote Originally Posted by Aldane View Post
    It try to switch panels by adding a new one and removing the old one
    Ever heard of CardLayout?

    db

Similar Threads

  1. jpanel in jframe-pls help
    By archanaanbu in forum Java Applets
    Replies: 3
    Last Post: 02-14-2011, 05:01 AM
  2. How do you remove the X in the Title Bar in a Jframe
    By RickAintree in forum New To Java
    Replies: 5
    Last Post: 02-08-2011, 04:15 PM
  3. jList - add/remove items and query - using jFrame Form
    By ArneBassez in forum AWT / Swing
    Replies: 2
    Last Post: 12-01-2010, 12:42 PM
  4. Right-clicking a rectangle on a JFrame to remove it
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-20-2009, 02:44 AM
  5. how to remove all component in container(Jpanel)
    By SaYuNaRa in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 04:22 AM

Tags for this Thread

Posting Permissions

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