0

I am trying to change the JPanel in the parent class to the JPanel in the child class once the Jbutton jbtcredits is clicked.I tried implementing cardlayout in the child class but however,instead of the old JFrame changing the old JPanel to the new one,a new JFrame pops out with the new JPanel and the old JFrame with the old JPanel is left unchanged.This is the parent class:

private JButton jbtstart= new JButton("Start Cooking!");
private JButton jbtabout = new JButton("About");
 JButton jbtcredits = new JButton("Credits");
private JButton jbtexit = new JButton("Exit");
private JLabel Screen;
JFrame frame = new JFrame("f");
JPanel p1 = new JPanel();
JPanel p = new JPanel();
JLabel back=new JLabel(new ImageIcon("C:\\Users\\Desktop\\background.png"));

//private static int gridSize = 2;
public GUI_interface()
{
    super("GUIinterface");
}        
public void createAndDisplayGUI()
{   
    Screen=new JLabel(new ImageIcon("C:\\Users\\Desktop\\Title.png"));      
    jbtstart.setLayout(new BorderLayout());                             
    //JPanel p = new JPanel();      
    //p.setLayout(null);                        
    p1.setLayout(new GridLayout(1,1));        
    p.setLayout(new BorderLayout());        
    back.setLayout(new FlowLayout());        
    jbtstart.setPreferredSize(new Dimension(40, 40));       

    p.add(Screen,BorderLayout.NORTH);
    p.add(jbtstart);
    p1.add(jbtabout);
    p1.add(jbtcredits);
    p1.add(jbtexit);

    back.add(p);
    back.add(p1);

    jbtstart.addActionListener(this);
    jbtabout.addActionListener(this);
    jbtcredits.addActionListener(this);
    jbtexit.addActionListener(this);


    setContentPane(back);

   frame.setTitle("Cooking App");

   frame.setLocation(300,250);
   frame.setResizable(false);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.add(back);
   frame.pack();
   frame.setLocationByPlatform(true);
   frame.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
// find out which button was pressed

 if (e.getSource()==jbtstart)
  {
     JOptionPane.showMessageDialog(null,"to be added");
  }
  else if (e.getSource() == jbtabout)
  {
      About files=new About();
      //files.setSize(20, 30);
      files.createAndDisplayGUI();
  }
  else if (e.getSource() == jbtcredits)
  {

     Credits filess=new Credits();
     filess.createAndDisplayGUI();

 }
  else
  {

      { Object[] options = {"Yes","No"};
        int n = JOptionPane.showOptionDialog(null,
                       "Are you sure you would like to quit? ","Cooking App",
                       JOptionPane.PLAIN_MESSAGE,
                       JOptionPane.QUESTION_MESSAGE,
                       null,
                       options,
                       options[1]);
        if (n==0)
        {
            System.exit(0);
        }
      }
  }





public static void main(String[] args)
{

    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            new GUI_interface().createAndDisplayGUI();
        }
    });
}

This is the child class with the new JPanel:

private JButton jbtback = new JButton("back");
JPanel a =new JPanel();
JPanel b=new JPanel();
public void createAndDisplayGUI(){

 CardLayout c1= new CardLayout();
a.setLayout(c1);
JLabel back1=new JLabel(new ImageIcon("C:\\Users\\Desktop\\background.png"));

b.add(back1);
b.add(jbtback);

a.add(back,"1");
a.add(b,"2");
c1.show(a,"2");

setContentPane(a);

//a.add(back,"1");
a.add(jbtback,"2");

jbtcredits.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
// find out which button was pressed

    c1.show(a,"2");  
  }
});


jbtback.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e)
    {
    // find out which button was pressed

        c1.show(a,"1");  
      }
    });

frame.add(a);
frame.setTitle("About us");
frame.setSize(300,250);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);



}

}

1 Answers1

0

This creates a new JFrame:

Credits filess=new Credits();

If you use static access to frame

Old:

JFrame frame = new JFrame("f");

New:

static JFrame frame = new JFrame("f");

There will be only one JFrame :)

PS: Update your frame with

frame.invalidate();
frame.repaint();

PSPS: You can use action-commands, you don't have to compare Objects like (e.getSource() == jbtcredits).

Example:

 btn.setActionCommand("Show credits");

 ....
 public void actionPerformed(ActionEvent actionEvent){

  if("Show credits".equals(actionEvent.getActionCommand())){

  }
 }
  • I tried it and it works.But however after clicking the credits button,the old buttons appear when i hover over the screen.I added the update and it works two tries later – Ambujam Iyengar Aug 11 '14 at 14:07
  • Superb ! :) If it works and you are satisfied, can you marks this post as solved :)? Thank you . – user2667549 Aug 12 '14 at 06:39
  • Hi but im not sure where to add the validate statements.Because the JFrame is not updating.I forgot to save the work overnight and now i cant seem to figure it out.I need this tiny help and i will be done.:) – Ambujam Iyengar Aug 12 '14 at 15:50
  • In your action performed, after you updated your View :). [invalidate vs repaint](http://stackoverflow.com/questions/4396583/java-swing-repaint-vs-invalidate) – user2667549 Aug 13 '14 at 06:20