1 Replies - 572 Views - Last Post: 27 July 2013 - 06:14 AM Rate Topic: -----

#1 Badooda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-July 13

CardPanel error: wrong parent for CardLayout

Posted 27 July 2013 - 04:10 AM

Hello everyone I have just now tried to get used to cardPanels but I am having a few problems.

In my main class TicTacToe I have a method which will switch the cardpanels, the code is:
        public void setCurrentPanel(){
        cardlayout.show(cardPanel, "2");
        System.out.println("It's working");
    }
(As you will see later on)

That method works to use in that class but I want to acces it with separate class and for that I have created an object of TicTacToe named "tac" and to execute the method "SetCurrentPanel()" I use this simple code:
tac.setCurrentPanel();
I am able to acces the method, I know that since the print line "It's working" shows up when I'm deleting the "cardlayout.show(cardPanel, "2");" code.

What am I doing wrong? why can't I use "cardlayout.show(cardPanel, "2");" when calling it from another class?


---The full code---


The main code:
package tictactoe;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;



public class TicTacToe extends JFrame{
    public CardLayout cardlayout = new CardLayout();
    public static JPanel cardPanel, buttonPanel;
    private JLabel jl1, jl2;
    private JButton btn1, btn2;
    public TheActualMenu menu;
    public WindowMenu gameSession;
    public TicTacToe taca;
    
    
    
    
    
    public void IniTicTacToe(){
        
        
        menu = new TheActualMenu();
        gameSession = new WindowMenu();
        
        cardPanel = new JPanel();
        cardPanel.setLayout(cardlayout);
        buttonPanel = new JPanel();
        cardPanel.add(menu, "1");
        cardPanel.add(gameSession, "2");
        cardlayout.show(cardPanel, "1");
        btn1 = new JButton("Show Card 1");
        
        btn1.addActionListener(new ActionListener(){
            @Override
           public void actionPerformed(ActionEvent e){
               cardlayout.show(cardPanel, "1");
           } 
        });
        
        
        
        
        btn2 = new JButton("Show Card 2");
        btn2.addActionListener(new ActionListener(){ 
            @Override
            public void actionPerformed(ActionEvent e){
                setCurrentPanel();
            }
        });
        
        
        
        buttonPanel.add(btn1);
        buttonPanel.add(btn2);
        add(buttonPanel, BorderLayout.SOUTH);
        
        
        
        
        
    }
    
    
    public void setCurrentPanel(){
        cardlayout.show(cardPanel, "2");
        System.out.println("It's working");
    }
    
    
    
    
    
    
    public static void main(String[] args) {   
        TicTacToe tictac = new TicTacToe();
          tictac.IniTicTacToe();
          JFrame window = new JFrame("Tic tac toe");
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
          window.add(cardPanel);
          window.setSize(966,600);
          window.setVisible(true);

    }

    }




The code which should shange the panels when the right event happens:
package tictactoe;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;



public class TheActualMenu extends JPanel{
    private Image background;
    private Image StartNormalState;
    private Image StartMouseHover;
    private Image StartPressed;
    private Image imageState;
    

    
    
    
    
    
    public TheActualMenu(){
        background = new ImageIcon("C:\\Users\\MyName\\Desktop\\placeholder\\TicTacToe\\TicTacToe\\src\\tictactoe\\TTTBackground.png").getImage();
        StartNormalState = new ImageIcon("C:\\Users\\MyName\\Desktop\\placeholder\\TicTacToe\\TicTacToe\\src\\tictactoe\\StartNotPressed.png").getImage();
        StartMouseHover = new ImageIcon("C:\\Users\\MyName\\Desktop\\placeholder\\TicTacToe\\TicTacToe\\src\\tictactoe\\StartMouseHover.png").getImage();
        StartPressed = new ImageIcon("C:\\Users\\MyName\\Desktop\\placeholder\\TicTacToe\\TicTacToe\\src\\tictactoe\\StartMousePressed.png").getImage();
        imageState = StartNormalState;
        
        
        Handler theHandler = new Handler();
        
    }
    
    
    @Override
    public void paint(Graphics g){
        super.paint(g);
        g.drawImage(background, 0, 0, null);
        g.drawImage(imageState, (966/2)-(imageState.getWidth(this)/2), 32, null);
    }
    
    
    
    
    
    class Handler extends MouseAdapter{
        private TicTacToe tac;
        
        
        public Handler(){
            tac = new TicTacToe();
            
            
            
        
        addMouseMotionListener(new MouseAdapter(){ 
            @Override
            public void mouseMoved(MouseEvent e){
                if(e.getX() >= (966/2)-(imageState.getWidth(null)/2) 
                        && e.getX() <= (966/2)+(imageState.getWidth(null)/2) 
                        && e.getY() >= 32 && e.getY() <= 32+imageState.getHeight(null)){
                imageState = StartMouseHover;
                repaint();
                }
                else
                {
                imageState = StartNormalState;
                repaint();
                }
            }
        });
        
        addMouseListener(new MouseAdapter(){
            @Override
            public void mousePressed(MouseEvent e){
                if(e.getX() >= (966/2)-(imageState.getWidth(null)/2) 
                        && e.getX() <= (966/2)+(imageState.getWidth(null)/2) 
                        && e.getY() >= 32 && e.getY() <= 32+imageState.getHeight(null)){
                    imageState = StartPressed;
                    repaint();
                }
            }
        });
        
        
        
        addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                if(e.getX() >= (966/2)-(imageState.getWidth(null)/2) 
                        && e.getX() <= (966/2)+(imageState.getWidth(null)/2) 
                        && e.getY() >= 32 && e.getY() <= 32+imageState.getHeight(null)){
                    tac.setCurrentPanel();
                    //EVENT WHERE THE CHANGING SHOULD HAPPEN
                }
                
            }
        });
        
        
        }
        
        
        
        
        
    }
    
    
    
    
    
    
    
}






The error message:

Quote

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong parent for CardLayout
at java.awt.CardLayout.checkLayout(CardLayout.java:384)
at java.awt.CardLayout.show(CardLayout.java:506)
at tictactoe.TicTacToe.setCurrentPanel(TicTacToe.java:72)
at tictactoe.TheActualMenu$Handler$3.mouseClicked(TheActualMenu.java:95)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
at java.awt.Component.processMouseEvent(Component.java:6300)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6062)
at java.awt.Container.processEvent(Container.java:2039)
at java.awt.Component.dispatchEventImpl(Component.java:4660)
at java.awt.Container.dispatchEventImpl(Container.java:2097)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4245)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
at java.awt.Container.dispatchEventImpl(Container.java:2083)
at java.awt.window.dispatchEventImpl(window.java:2489)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:674)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:633)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:647)
at java.awt.EventQueue$3.run(EventQueue.java:645)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:644)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: CardPanel error: wrong parent for CardLayout

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,026
  • Joined: 05-April 11

Re: CardPanel error: wrong parent for CardLayout

Posted 27 July 2013 - 06:14 AM

Something is very wrong with your design

You create a TicTacToe, which adds a TheActualMenu
TheActualMenu creates a Handler, which creates a new TicTacToe
You are very close to creating a loop :) Luckly for you, the iniTicTacToe method doesn't get called

Pass the TicTacToe to Handler and don't make it create a new instance

This post has been edited by CasiOo: 27 July 2013 - 06:27 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1