2

I have found that there are many similar topics here, but my problem is something more complicated.

Background of my problem:-

I have a JFrame called Main. On this JFrame I have two buttons and one JPanel called WorkingPanel. Then I have another JPanel(called PlayerPanel) but this one is a seprate file (as a class).

Now I want that when I click a button, it should change WorkingPanel to PlayerPanel. I have wrote following code.

private void MenuButtonPlayerViewMouseClicked(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
    WorkingPanel = new PlayerPanel();
    System.out.println(WorkingPanel.getName());
    WorkingPanel.revalidate();
    WorkingPanel.repaint();

    WorkingPanel.setVisible(true);

    Window.revalidate();
    Window.repaint();
}    

Please guide me, Thanks.

Junejo
  • 717
  • 1
  • 7
  • 14
  • 1
    Use standard Java naming conventions is you want people to read your code. Variable names do NOT start with an upper case character. Class names DO start with an upper case character, so you code is very confusing to read. Follow guidelines from your text book or tutorials. Don't make up your own! – camickr Mar 22 '14 at 15:13

3 Answers3

4

I have found that there are many similar topics here, but my problem is something more complicated.

On the contrary, your description is of a rather basic problem that is very easily solved by using a CardLayout. I suggest that you do this now. If you had it in place your method could be as simple as:

private void MenuButtonPlayerViewMouseClicked(java.awt.event.MouseEvent evt) {
   cardLayout.show(cardPanel, WORKING_PANEL);
} 

where cardLayout is your CardLayout variable, cardPanel is the JPanel that displays the "cards" that displays the swapping JPanels, and WORKING_PANEL is a String constant that you used when you added your WorkingPanel instance to the cardPanel.


Point 2:

Don't use a MouseListener on a JButton as it won't behave correctly. For instance, if you disable the button via setEnabled(true) the button won't truly be disabled. Instead use an ActionListener with JButtons as the tutorials will show you. That is what they are for.


Edit
For examples of CardLayout-using GUI's, please check out:

This one is unusual in that it uses a CardLayout and has one panel fading into the other panel:

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
4

You could use CardLayout instead of that approach. You will be able to switch between different panels very easy and efficiently. It's also wort of mentioning that use of CardLayout is less verbose approach.

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
  • I am using Netbeans visual designer, how can I use CardLayout with it? – Junejo Mar 22 '14 at 13:26
  • 1
    By writing your code with hands. **EDIT:** Actually, you can set it very easy in NetBeans for some `JPanel`. Just right-click on desired `JPanel` > Set Layout > CardLayout – Branislav Lazic Mar 22 '14 at 13:26
  • 1
    @ZulfiqarJunejo: I strongly urge you not to use a code generator until you understand the library that you're using. Code by hand first to gain that understanding. – Hovercraft Full Of Eels Mar 22 '14 at 13:35
  • 1
    @ZulfiqarJunejo: case in point, you're using a MouseListener on a JButton, something that you should almost never do, and something that the Oracle Swing tutorials will tell you. Please have a look at the tutorials. – Hovercraft Full Of Eels Mar 22 '14 at 13:40
3

Use a CardLayout, containing your two panels, but only showing one at a time. The CardLayout is documented, with examples, in the Swing tutorial.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255