Results 1 to 5 of 5
- 12-07-2010, 07:47 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 8
- Rep Power
- 0
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:
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:Menu menu = new Menu(); removeAll(); add( menu); menu.requestFocus(); menu.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.PHP Code:Help helpPanel = new Help(); removeAll(); add( helpPanel); helpPanel.setVisible(true); repaint();
Here, if you want to see what I mean:
-
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!
- 12-07-2010, 10:22 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 8
- Rep Power
- 0
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)
- 12-08-2010, 06:00 PM #5
Member
- Join Date
- Jan 2009
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Java Paint Program problem (JPanel)
By KilKidd in forum Advanced JavaReplies: 6Last Post: 11-20-2010, 04:31 AM -
Need to Save a data to file from JPanel and read the data back to Jpanel
By yashrajsen in forum AWT / SwingReplies: 1Last Post: 11-09-2010, 09:28 AM -
I need some help with JPanel
By bantes in forum AWT / SwingReplies: 7Last Post: 11-03-2009, 07:58 PM -
paint vs paintComponent for a JPanel
By lightstream in forum AWT / SwingReplies: 4Last Post: 01-29-2009, 02:26 AM -
help?! paint on top of swing components on JPanel
By beam2008 in forum AWT / SwingReplies: 1Last Post: 12-05-2008, 04:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks