Re: JButton open new window
Quote:
i want to click a button in my first menu and have it open my other menu i made.
For the program to execute a method in response to a click on a button, the button needs to have a listener added to it. So first you should work on getting that to work.
Where do you want the MyPanel2 class to be viewed when it is created? In a new JFrame or where? It has to be added to a container that displays on the screen. A JPanel does not display by itself.
BTW The varaible names don't reflect the purpose and usage of the components and makes the code harder to read: jcompX ??? Why not give them meaningful names: testBtn1, openBtn etc
Re: JButton open new window
1. for the class with the JButton put "implements MouseListener" after it says "extends JPanel" and import the mouse listener class
2. in the constructor write, this.addMouseListener(this); and for your button write myButton.addMouseListener(this); replacing myButton with whatever yours is called.
3. then in the actionPerformedMouseClicked method that you must have because you're implementing MouseListener, have a check for the button. to see if its the one you want to make open the other menu, would look something like this, "if(e.getComponent().equals(myButton)) //open menu;
4. Not sure if you want a whole new window or just to replace whats in your JFrame, if you want a whole new window in the if statement mentioned above instantiate the class that extends JFrame holds your other menu, it would look something like the first one. If you want the Panel to be switched just change what panel is displayed in the original JFrame