I have a extremely small problem, I think its my own fault, but i just cant seem to find what it is. I'm making a program with a gui, and in the gui there is a main menu with 2 JButtons. When I click on a JButton the JPanel/JFrame is totally cleared and some other JButtons are displayed. One of these JButtons says Main Menu, so when it gets clicked, I want it to go back to the Main Menu, but nothing happens. I think it is a problem with the way I'm trying to make the program go to the method new AnimalProfitRatioCalc (); in public void actionPerformed (ActionEvent e)

here is my code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;


/*
*Created by 
*on Jan 4
*to Calculate the profit ratio of animals and display them in sorted order
*/

public class AnimalProfitRatioCalc extends JFrame implements ActionListener
{

    static Container contentPane;
    static JPanel infoPane;

    
    public AnimalProfitRatioCalc ()
    {


        contentPane = getContentPane ();
        if (infoPane != null)
        {
            contentPane.remove (infoPane);
        }
        infoPane = new JPanel ();
        infoPane.setLayout (null); //setting layout to null so i can use absolute positioning with coordinates


        JMenuBar MenuBar;
        JMenu menu;
        JMenuItem MenuItem;
        MenuBar = new JMenuBar ();
        setJMenuBar (MenuBar);

        //adding menu
        menu = new JMenu ("File");
        MenuBar.add (menu);
        MenuItem = new JMenuItem ("Quit");
        MenuItem.addActionListener (this);
        menu.add (MenuItem);
        menu = new JMenu ("Options");
        MenuItem.addActionListener (this);
        MenuBar.add (menu);
        MenuItem = new JMenuItem ("Main Menu");
        MenuItem.setToolTipText ("Go to main menu"); //tip
        MenuItem.addActionListener (this);
        menu.add (MenuItem);
        MenuItem = new JMenuItem ("Enter Animal Statistics");
        MenuItem.setToolTipText ("Animal category, Breed, Acquisition price, selling price "); //tip
        MenuItem.addActionListener (this);
        menu.add (MenuItem);
        MenuItem = new JMenuItem ("Display Animals with profit ratio");
        MenuItem.setToolTipText ("Display Animals with preferred profit ratio in sorted order"); //tip
        MenuItem.addActionListener (this);
        menu.add (MenuItem);



        JLabel L1;
        //all the JLabels
        L1 = new JLabel ("Animal Profit Ratio Calculator");
        L1.setFont (new Font ("Arial", Font.BOLD, 24));
        L1.setBounds (232, 10, 342, 20);
        infoPane.add (L1);



        //all JButtons
        JButton eas, da;

        eas = new JButton ("Enter Animal Statistics");
        eas.setToolTipText ("Animal category, Breed, Acquisition price, selling price "); //tip
        eas.setFont (new Font ("Arial", Font.BOLD, 18));
        eas.addActionListener (this);
        eas.setBounds (275, 75, 256, 50);
        infoPane.add (eas);

        da = new JButton ("Display Animals with profit ratio");
        da.setToolTipText ("Display Animals with preferred profit ratio in sorted order"); //tip
        da.setFont (new Font ("Arial", Font.BOLD, 18));
        da.addActionListener (this);
        da.setBounds (246, 175, 312, 50);
        infoPane.add (da);

        contentPane.add (infoPane);
        validate ();
    }


    public void actionPerformed (ActionEvent e)
    {
        String event = e.getActionCommand ();
        if (event.equals ("Quit"))
        {
            hide ();
            System.exit (0);
        }
        if (event.equals ("Enter Animal Statistics"))
        {
            AddStatistics ();
        }
        if (event.equals ("Display Animals with profit ratio"))
        {
            DisplayStatistics ();
        }
        if (event.equals ("Main Menu"))
        {
            new AnimalProfitRatioCalc ();
        }
    }


    public void AddStatistics ()
    {
        contentPane = getContentPane ();
        if (infoPane != null)
        {
            contentPane.remove (infoPane);
        }
        infoPane = new JPanel ();
        infoPane.setLayout (null); //setting layout to null so i can use absolute positioning with

        JLabel L1, L2, L3, L4, L5;

        //all the  JLabels
        L1 = new JLabel ("Enter Animal Statistics");
        L1.setFont (new Font ("Arial", Font.BOLD, 24));
        L1.setBounds (272, 10, 261, 20);
        infoPane.add (L1);

        L2 = new JLabel ("Animal Catagory");
        L2.setFont (new Font ("Arial", Font.PLAIN, 18));
        L2.setBounds (125, 100, 261, 20);
        infoPane.add (L2);

        L3 = new JLabel ("Animal Breed");
        L3.setFont (new Font ("Arial", Font.PLAIN, 18));
        L3.setBounds (125, 150, 261, 20);
        infoPane.add (L3);

        L4 = new JLabel ("Acquisition Price");
        L4.setFont (new Font ("Arial", Font.PLAIN, 18));
        L4.setBounds (125, 200, 261, 20);
        infoPane.add (L4);

        L5 = new JLabel ("Selling Price");
        L5.setFont (new Font ("Arial", Font.PLAIN, 18));
        L5.setBounds (125, 250, 261, 20);
        infoPane.add (L5);


        JTextField J1, J2, J3, J4;
        //all JTextFields

        J1 = new JTextField (20);
        J1.setFont (new Font ("Arial", Font.PLAIN, 14));
        J1.setToolTipText ("Catagory"); //tip
        J1.setBounds (350, 100, 120, 20);
        infoPane.add (J1);

        J2 = new JTextField (20);
        J2.setFont (new Font ("Arial", Font.PLAIN, 14));
        J2.setToolTipText ("Breed"); //tip
        J2.setBounds (350, 150, 120, 20);
        infoPane.add (J2);

        J3 = new JTextField (20);
        J3.setFont (new Font ("Arial", Font.PLAIN, 14));
        J3.setToolTipText ("Acquisition Price"); //tip
        J3.setBounds (350, 200, 120, 20);
        infoPane.add (J3);

        J4 = new JTextField (20);
        J4.setFont (new Font ("Arial", Font.PLAIN, 14));
        J4.setToolTipText ("Selling Price"); //tip
        J4.setBounds (350, 250, 120, 20);
        infoPane.add (J4);

        JButton B1, B2;

        B1 = new JButton ("Add Animal");
        B1.setToolTipText ("Add animal to data base"); //tip
        B1.setFont (new Font ("Arial", Font.PLAIN, 18));
        B1.addActionListener (this);
        B1.setBounds (550, 100, 130, 170);
        infoPane.add (B1);

        B2 = new JButton ("Main Menu");
        B2.setFont (new Font ("Arial", Font.PLAIN, 18));
        B2.addActionListener (this);
        B2.setBounds (272, 300, 261, 50);
        infoPane.add (B2);
        contentPane.add (infoPane);
        validate ();
    }


    public void DisplayStatistics ()
    {

    }


    public static void main (String[] args)  //main
    {
        AnimalProfitRatioCalc window = new AnimalProfitRatioCalc ();
        window.setTitle ("Animal Profit Ratio Calculator");
        window.setSize (800, 600);
        window.setVisible (true);

    }



}

Edited 4 Years Ago by scheppy: n/a

What happens after you create a new instance of the AnimalProfitRatioCalc class in the button listener?
What do you expect to happen?

Look at the first place you create an instance of AnimalProfitRatioCalc. What does that code do?

What happens after you create a new instance of the AnimalProfitRatioCalc class in the button listener?
What do you expect to happen?

Look at the first place you create an instance of AnimalProfitRatioCalc. What does that code do?

I have no clue, I added new becouse it I get an error if I dont, now my code runs fine, but the JButton doesn't respond.

Is it becouse AnimalProfitRatioCalc implements ActionListener?
do I have to make a seperate method with all the main menu code?

1) look how LayoutManager works

2) use CardLayout instead of AbsoluteLayout

3) read tutorial about ActionListener, JButton and JMenu,

4) if you don't want to bothering with CardLayout, then for swaping (remove then add) betweens JComponents you have to call

revalidate();
repaint();//most cases required

5) follows examples from linked tutorials

Comments
It might not be the answer but he made the effort to help
not what im asking

but the JButton doesn't respond.

Add a println to the AnimalProfitRatioCalc class's constructor to see if it is called when you press the button.
Then go back and re-read my previous post.

Comments
Like the way you dont just give awsers but make people learn themselves and help them find thier own mistakes

Add a println to the AnimalProfitRatioCalc class's constructor to see if it is called when you press the button.
Then go back and re-read my previous post.

I added a println after public AnimalProfitRatioCalc () and yes it print's.
so it is called.

and I dont understand what you mean in your other post

Add a println to the AnimalProfitRatioCalc class's constructor to see if it is called when you press the button.
Then go back and re-read my previous post.

Ok thanks alot NormR1
I just kept on looking at my code
and then I saw it,

Here is my fixed code
thanks alot

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;


/*
*Created by ****** ****
*on Jan 4
*to Calculate the profit ratio of animals and display them in sorted order
*/

public class AnimalProfitRatioCalc extends JFrame implements ActionListener
{

    static Container contentPane;
    static JPanel infoPane;
    static int numberofanimals = 0;





    public AnimalProfitRatioCalc ()
    {

        JMenuBar MenuBar;
        JMenu menu;
        JMenuItem MenuItem;
        MenuBar = new JMenuBar ();
        setJMenuBar (MenuBar);

        //adding menu
        menu = new JMenu ("File");
        MenuBar.add (menu);
        MenuItem = new JMenuItem ("Quit");
        MenuItem.addActionListener (this);
        menu.add (MenuItem);
        menu = new JMenu ("Options");
        MenuItem.addActionListener (this);
        MenuBar.add (menu);
        MenuItem = new JMenuItem ("Main Menu");
        MenuItem.setToolTipText ("Go to main menu"); //tip
        MenuItem.addActionListener (this);
        menu.add (MenuItem);
        MenuItem = new JMenuItem ("Enter Animal Statistics");
        MenuItem.setToolTipText ("Animal category, Breed, Acquisition price, selling price "); //tip
        MenuItem.addActionListener (this);
        menu.add (MenuItem);
        MenuItem = new JMenuItem ("Display Animals with profit ratio");
        MenuItem.setToolTipText ("Display Animals with preferred profit ratio in sorted order"); //tip
        MenuItem.addActionListener (this);
        menu.add (MenuItem);
        MainMenu ();
    }


    public void MainMenu ()
    {

        contentPane = getContentPane ();
        if (infoPane != null)
        {
            contentPane.remove (infoPane);
        }
        infoPane = new JPanel ();
        infoPane.setLayout (null); //setting layout to null so i can use absolute positioning with coordinates


        //all the JLabels
        JLabel L1;

        L1 = new JLabel ("Animal Profit Ratio Calculator");
        L1.setFont (new Font ("Arial", Font.BOLD, 24));
        L1.setBounds (232, 10, 342, 20);
        infoPane.add (L1);


        //all JButtons
        JButton eas, da;

        eas = new JButton ("Enter Animal Statistics");
        eas.setToolTipText ("Animal category, Breed, Acquisition price, selling price "); //tip
        eas.setFont (new Font ("Arial", Font.BOLD, 18));
        eas.addActionListener (this);
        eas.setBounds (275, 75, 256, 50);
        infoPane.add (eas);

        da = new JButton ("Display Animals with profit ratio");
        da.setToolTipText ("Display Animals with preferred profit ratio in sorted order"); //tip
        da.setFont (new Font ("Arial", Font.BOLD, 18));
        da.addActionListener (this);
        da.setBounds (246, 175, 312, 50);
        infoPane.add (da);

        contentPane.add (infoPane);
        validate ();
    }


    public void actionPerformed (ActionEvent e)
    {
        String event = e.getActionCommand ();
        if (event.equals ("Quit"))
        {
            hide ();
            System.exit (0);
        }


        if (event.equals ("Enter Animal Statistics"))
        {
            AddStatistics ();
        }


        if (event.equals ("Display Animals with profit ratio"))
        {
            DisplayStatistics ();
        }


        if (event.equals ("Main Menu"))
        {
            MainMenu ();
        }
    }


    public void AddStatistics ()
    {
        contentPane = getContentPane ();
        if (infoPane != null)
        {
            contentPane.remove (infoPane);
        }
        infoPane = new JPanel ();
        infoPane.setLayout (null); //setting layout to null so i can use absolute positioning with


        //all the  JLabels
        JLabel L1, L2, L3, L4, L5;

        L1 = new JLabel ("Enter Animal Statistics");
        L1.setFont (new Font ("Arial", Font.BOLD, 24));
        L1.setBounds (272, 10, 261, 20);
        infoPane.add (L1);

        L2 = new JLabel ("Animal Catagory");
        L2.setFont (new Font ("Arial", Font.PLAIN, 18));
        L2.setBounds (125, 100, 261, 20);
        infoPane.add (L2);

        L3 = new JLabel ("Animal Breed");
        L3.setFont (new Font ("Arial", Font.PLAIN, 18));
        L3.setBounds (125, 150, 261, 20);
        infoPane.add (L3);

        L4 = new JLabel ("Acquisition Price");
        L4.setFont (new Font ("Arial", Font.PLAIN, 18));
        L4.setBounds (125, 200, 261, 20);
        infoPane.add (L4);

        L5 = new JLabel ("Selling Price");
        L5.setFont (new Font ("Arial", Font.PLAIN, 18));
        L5.setBounds (125, 250, 261, 20);
        infoPane.add (L5);


        //all JTextFields
        JTextField J1, J2, J3, J4;

        J1 = new JTextField (20);
        J1.setFont (new Font ("Arial", Font.PLAIN, 14));
        J1.setToolTipText ("Catagory"); //tip
        J1.setBounds (350, 100, 120, 20);
        infoPane.add (J1);

        J2 = new JTextField (20);
        J2.setFont (new Font ("Arial", Font.PLAIN, 14));
        J2.setToolTipText ("Breed"); //tip
        J2.setBounds (350, 150, 120, 20);
        infoPane.add (J2);

        J3 = new JTextField (20);
        J3.setFont (new Font ("Arial", Font.PLAIN, 14));
        J3.setToolTipText ("Acquisition Price"); //tip
        J3.setBounds (350, 200, 120, 20);
        infoPane.add (J3);

        J4 = new JTextField (20);
        J4.setFont (new Font ("Arial", Font.PLAIN, 14));
        J4.setToolTipText ("Selling Price"); //tip
        J4.setBounds (350, 250, 120, 20);
        infoPane.add (J4);


        //all JButtons
        JButton B1, B2;

        B1 = new JButton ("Add Animal");
        B1.setToolTipText ("Add animal to data base"); //tip
        B1.setFont (new Font ("Arial", Font.PLAIN, 18));
        B1.addActionListener (this);
        B1.setBounds (550, 100, 130, 170);
        infoPane.add (B1);

        B2 = new JButton ("Main Menu");
        B2.setFont (new Font ("Arial", Font.PLAIN, 18));
        B2.addActionListener (this);
        B2.setBounds (272, 300, 261, 50);
        infoPane.add (B2);
        contentPane.add (infoPane);
        validate ();
    }


    public void DisplayStatistics ()
    {

    }


    public void AddAnimal ()
    {
        numberofanimals = numberofanimals + 1;
    }


    public static void main (String[] args)  //main
    {
        AnimalProfitRatioCalc window = new AnimalProfitRatioCalc ();
        window.setTitle ("Animal Profit Ratio Calculator");
        window.setSize (800, 600);
        window.setVisible (true);

    }



}
This question has already been answered. Start a new discussion instead.