Results 1 to 5 of 5
  1. #1
    scheffetz is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default Problems calling JPanel

    I am trying to build the navigation though my GUI.
    On my first screen I have a few options that you can select. When you select the option is sets the new JPanel. From the new JPanel I can navigate back to my original screen as well.

    Where my problem lies is when I try to navigate from the 1st JPanel to a 2nd JPanel. I cant figure out what I am doing wrong.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class MenuGUI extends JFrame implements ActionListener {
    
    CategoryGUI myCat;
    
    public MenuGUI(){
            
            myCat = new CategoryGUI();
            myCat.backButton.addActionListener(this);
            c = getContentPane();
            contentPane = getContentPane();
     	contentPane.setLayout(null);
    
            declare my buttons and labels and stuff here
    }
    
    public void actionPerformed(ActionEvent e){
    
    if(startButton == e.getSource()){
                soundclip = "assets/sounds/click.wav";
                Thread thread1 = new Thread(new soundClip(soundclip), "soundclip");
                thread1.start();
    
                setContentPane(myCat);
                myCat.setVisible(true);
            }
    
    if(myCat.backButton == e.getSource()){
                 this.setContentPane(c);
            }
    }
    }
    .
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class CategoryGUI extends JPanel implements ActionListener{
    
    DifficultyFlags flagD;
    
    public CategoryGUI(){
            contentPane = this;
     	contentPane.setLayout(null);
            setSize(1100, 700);
            flagD = new DifficultyFlags();
    
           declare my buttons and labels and stuff here
    }
    
     public void actionPerformed(ActionEvent e){
    
    if(backButton == e.getSource()){
                soundclip = "assets/sounds/click.wav";
                Thread thread1 = new Thread(new soundClip(soundclip), "soundclip");
                thread1.start();
            }
    
    if(flagButton == e.getSource()){
                soundclip = "assets/sounds/click.wav";
                Thread thread1 = new Thread(new soundClip(soundclip), "soundclip");
                thread1.start();
               
                setContentPane(flagD);      <--- ERROR HERE
                flagD.setVisible(true);
    
            }
    }
    DifficultyFlag is also a class that extends JPanel. It is the same in structure as Category GUI.
    I receive an error where I try to set ContentPane to the new JPanel.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    What error are you getting? That's some pertinent information.

    Hint- JPanel doesn't have a setContentPane() method.

    Besides, CardLayout is your friend here.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    scheffetz is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    the error is:

    cannot find symbol
    symbol: method setContentPane(codejunkies.DifficultyFlags)
    location: class codejunkies.CategoryGUI

    which makes sense I suppose seeing as you said JPanel does not have setContentPane

    I really cant go the Cardlayout route because my project is almost due and its nearly complete as well. I just dont have to the time to really learn it and make big changes.

    I might not have done things the most optimal way, but its a learning experience and next project will be different im sure.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Quote Originally Posted by scheffetz View Post
    the error is:

    cannot find symbol
    symbol: method setContentPane(codejunkies.DifficultyFlags)
    location: class codejunkies.CategoryGUI

    which makes sense I suppose seeing as you said JPanel does not have setContentPane

    I really cant go the Cardlayout route because my project is almost due and its nearly complete as well. I just dont have to the time to really learn it and make big changes.

    I might not have done things the most optimal way, but its a learning experience and next project will be different im sure.
    Do you have any further questions then? The correct way to do this is with a CardLayout, which really shouldn't take long, but it's your call. The problem is that you're trying to set the content pane of a JPanel, which doesn't make sense. Do you have it figured out now?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    scheffetz is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    I did figure it out.

    It took me a while, java comes a bit slowly to me.

Similar Threads

  1. Problems with a loop calling data from an ArrayList.
    By moriarty in forum New To Java
    Replies: 30
    Last Post: 03-28-2010, 04:00 AM
  2. Replies: 1
    Last Post: 02-19-2010, 11:35 AM
  3. Replies: 3
    Last Post: 04-21-2009, 12:31 AM
  4. Problems with readLine() and calling methods
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 08:44 AM
  5. JPanel Problems
    By Riftwalker in forum AWT / Swing
    Replies: 6
    Last Post: 10-16-2007, 12:16 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
  •