-1

Im developing a project in which I have "Class1", "Class2", and "Class3". Class2 and Class3 both create JFrame's, each containing various JButton's, JLabel's, and other swing components. How do i make so in Class1 I can reffrence the JButton from Class2 and use an action listener to set the Class2's visibilty to false and Class3's visibilty to true.

I Tried This: Setting Class2 to visbile in my main method was no issue, but once i started to implement Class3 things didint work.

Summary: Having Issues initiating a jbutton from an other class and using an action listener that refrences that jbutton.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Class1 extends JFrame implements ActionListener
{

    public static void main(String[] args) {
        Class2 frameFromclass2 = new Class2();

        frameFromclass2.setVisible(true);
    }       
    Class2 buttonMovetoclass3 = new Class2();

    public void actionPerformed(ActionEvent e) {

        if (buttonMovetoclass3 == e.getSource()) {   
            Class2 frameFromclass2 = new Class2();

            frameFromclass2.setVisible(false);

            Class3 frameFromclass3 = new Class3();

            frameFromclass3.setVisible(true);

                        
        }
    }   
    
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Evan
  • 13
  • 2
  • Can you show your main class? We need to know how your JFrame classes are accessing each other. Also, for the record, it is less confusing if the various classes/variables are not named "Class1, Class2, Class3" and instead named things like "SettingsWindow, PrimaryWindow, CatPicsWindow" or whatever. Helps your readers keep better track of what's going on. – Blue Dev Mar 20 '23 at 18:17
  • 2
    "*Class2 and Class3 both create JFrame's*" maybe instead of having many JFrames create one and use something like [Tabbed Panes](https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html) to show only one panel at a time. The other classes may simply be JPanes which will be assigned to different tabs. – Pshemo Mar 20 '23 at 18:54
  • An application should only have a single JFrame. If you need child windows then you can use a JDialog. – camickr Mar 20 '23 at 19:04
  • @BlueDev Thank You for the response! I used simpler names to help people who could awnser understand but now I see in reality it just made it more vague. Im starting a new project, and usually I would use the same class but for a potentially bigger project i split it up and am finding difficulty. I am making a starting screen that displays an app titile and signin/signup buttons. Right now I used eclipse to make all my JFrames so the visuals are fine. Any suggestions on how to actually transition from my start screen to my sign up screen using an action listener? – Evan Mar 20 '23 at 20:13
  • @BlueDev That is essentially my main classes code, (with diffrent varriables) my other classes just set up my swing components for the visuals. – Evan Mar 20 '23 at 20:14

1 Answers1

0

Part of your problem is caused by a lack of understanding in terms of variable scope. The other part of your problem is just in approaching this the wrong way.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Class1 extends JFrame implements ActionListener
{
    //This is a method. Every variable declared within this method, 
    //is only accessible from within this method. 
    public static void main(String[] args) {
        //That includes this one. 
        Class2 frameFromclass2 = new Class2();
        //You've used it here, but not only is it then abandoned, but it
        //cannot be used anywhere else. It is contained in a cage of curly braces. 
        frameFromclass2.setVisible(true);
    }       
    Class2 buttonMovetoclass3 = new Class2();

    public void actionPerformed(ActionEvent e) {

        if (buttonMovetoclass3 == e.getSource()) {   
            //Here you create a NEW Class2 object and give it the same
            //name you gave the other, then you hide it.
            Class2 frameFromclass2 = new Class2();

            frameFromclass2.setVisible(false);
            //And then you do the same thing here, but with Class3; then you show it. 
            Class3 frameFromclass3 = new Class3();

            frameFromclass3.setVisible(true);
        }
    }   
}

We aren't able to see what your other classes have in their constructors, so I can't say whether or not there's also a problem happening there. I can, however, offer better alternatives.

Generally you want to keep 1 JFrame, even if you have popup windows or multiple "pages" that can be used. Consider using multiple JPanels, one per layout. Alternatively you can use a JTabbedPane, and have different information on each tab. For login purposes, a common option is the JOptionPane or a JDialog. If you insist on using multiple JFrames and you want to move components between them, variables within your class (not a method) should be used to store information, then use public methods to access those variables from the other classes. Once a window is done being used, however, you should destroy it instead of hide it. Here's a great resource on various ways to do that.

Blue Dev
  • 142
  • 8
  • 1
    Thank You so much for your care and thoughtfullness In the comment. Im 16 just getting into coding my first big project and Its so cool that there are people out there willing to help guide me. Thanks Again, this helped me turn a important corner in the beggining of my first project. – Evan Mar 21 '23 at 23:52
  • I'm glad I could help! I was 15 when I began, over 11 years ago. Some people can be pretty harsh against newbies, but as long as you've done your research and tried everything you can think of, your questions will generally be well-received. Good luck and don't give up! – Blue Dev Apr 25 '23 at 18:12