Results 1 to 5 of 5
  1. #1
    Tanshaydar is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Default Placing a new JPanel over a paint overriden JPanel

    Hi, I have a term project where I'm coding a 2D game with Java.
    Now, I have the case as:
    I have a JPanel which implements ActionListener and overrides paint method.

    My JFrame starst with a Menu JPanel. I press the Play button, then Game JPanel takes the place and game starts, but after that, I press the ESC to go back to Menu JPanel. Problem starts here, when I create a new Menu JPanel, it doesn't appear at all. The JButtons over it doesn't appear until I hover over them. And background never changes.

    I call the Menu JPanel like this:
    PHP Code:
                Menu menu = new Menu();
                removeAll();
                add( menu);
                menu.requestFocus();
                menu.setVisible( true);
                repaint();
    This works, I know, because when I call another JPanel from the Menu JPanel (i.e. Help JPanel) it appears with this code:
    PHP Code:
            Help helpPanel = new Help();
            removeAll();
            add( helpPanel);
            helpPanel.setVisible(true);
            repaint();
    I think the repaint(); method that I call in Game JPanel has a conflict with the paint method I override in Game JPanel. I'm not into using CardLayout, because at this rate it's hard to implement.

    Here, if you want to see what I mean:

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

    Default

    Without more code, it'll be hard to guess what your problem is, but it could be that you're not calling revalidate on the container that holds the JPanels that you're swapping. If that container is the JFrame's contentPane, then you'll have to first cast it to a JPanel before calling this method, e.g.,

    Java Code:
    // swap your JPanels here
    
    JPanel contentPane = (JPanel)myJFrame.getContentPane(); 
    // or if class extends JFrame, then
    // JPanel contentPane = (JPanel)getContentPane(); 
    
    contentPane.revalidate();
    contentPane.repaint();

    But having said this, my main recommendation is not to swap JPanels like this but rather to use CardLayout to do the swapping for you.

    Luck!

  3. #3
    Tanshaydar is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Default

    My main JPanel is Menu Panel within a JFrame.
    When I press the Play JButton, it creates a new Game JPanel, then game starts. But when I press ESC, I want to hold game panel, and display the Menu JPanel and if I press the Continue JButton, I want to go back to Game JPanel.

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

    Default

    Quote Originally Posted by Tanshaydar View Post
    My main JPanel is Menu Panel within a JFrame.
    When I press the Play JButton, it creates a new Game JPanel, then game starts. But when I press ESC, I want to hold game panel, and display the Menu JPanel and if I press the Continue JButton, I want to go back to Game JPanel.
    Yep -- a CardLayout would work well for this. Either have a JPanel use CardLayout as it's layout and then add it to the JFrame's contentPane or have the contentPane itself use CardLayout. The tutorials will explain how to use it.

    How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

  5. #5
    Tanshaydar is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Default

    I solved the issue via override the paintComponent instead of paint itself.

Similar Threads

  1. Java Paint Program problem (JPanel)
    By KilKidd in forum Advanced Java
    Replies: 6
    Last Post: 11-20-2010, 04:31 AM
  2. Replies: 1
    Last Post: 11-09-2010, 09:28 AM
  3. I need some help with JPanel
    By bantes in forum AWT / Swing
    Replies: 7
    Last Post: 11-03-2009, 07:58 PM
  4. paint vs paintComponent for a JPanel
    By lightstream in forum AWT / Swing
    Replies: 4
    Last Post: 01-29-2009, 02:26 AM
  5. help?! paint on top of swing components on JPanel
    By beam2008 in forum AWT / Swing
    Replies: 1
    Last Post: 12-05-2008, 04:15 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
  •