-1

I have an ActionListener in a class extending JFrame. On button click I need to set the visibility of JFrame extended objects created in a Main class. In this example, clicking the button will make the existing object with that button become non-visible and it will make another JFrame object become visible.

The ActionListener is inside of one JFrame class, the object is created inside the Main class. How can I run the method of that specific object within the Main class from the ActionListener inside the JFrame class?

I already have a setVisibility method, but am unclear on how I can direct the ActionListener to run this method on an object that exists inside another class.

In Main Class:

Object1 jFrameObj1 = new Object1();

Object2 jFrameObj2 = new Object2();

In Object1 Class (inside action listener):

if(event.getSource() == button){

  jFrameObj1.setVisible(false);

  jFrameObj2.setVisible(true);

}
  • 2
    Please post an SSCCE. – JB Nizet Mar 23 '13 at 17:36
  • 1
    Ugh, another GUI that spits a bunch of JFrames at the user which is a terrible user interface design. The better solution: simply don't do this. Swap views with a CardLayout instead. Please also check out [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/522444) – Hovercraft Full Of Eels Mar 23 '13 at 17:38
  • Thank you. I will investigate CardLayout. I appreciate the advice. – user2202767 Mar 23 '13 at 17:46
  • Your setVisible above is being called in a static way on a class, not on an object, why? Your Java naming convention use is off too as variable names should begin with a lower-case letter. This may seem trivial, but after doing this for a while, you'll understand why it's not. – Hovercraft Full Of Eels Mar 23 '13 at 17:46
  • The method is a message in the strict OOP be sure you send it to the object you run. –  Mar 23 '13 at 17:55

1 Answers1

0

First of all, you should respect Java naming conventions, and choose much better names for your classes and variables. When you want to call a method on an object, you need a reference to this object. So the main method simply needs to pass a reference to the second frame to the first frame:

Object2 frame2 = new Object2();
Object1 frame1 = new Object1(frame2);

Then in the Object1 class:

private Object2 frameToDisplayWhenButtonIsClicked;

public Object2(frameToDisplayWhenButtonIsClicked) {
    this.frameToDisplayWhenButtonIsClicked;
}

...

public void actionPerformed(ActionEvent e) {
    setVisible(false);
    frameToDisplayWhenButtonIsClicked.setVisible(true);
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • As you can likely tell, I am a beginner in Java. Could you possibly provide some elaboration on your code example? I have been trying to implement it and am not sure I understand it correctly. – user2202767 Mar 23 '13 at 18:52
  • Is this what you are referring to? Main: SplashScreen splash = new SplashScreen(); JFrame: private SplashScreen splash; public SplashScreen(splash){ this.splash; } – user2202767 Mar 23 '13 at 18:54
  • 1
    You posted some code in your question, and I refactored this code to make it do what you asked for. If you don't understand what an object, a constructor and a field are, then you shouldn't try to use Swing yet. Learn the basics first. – JB Nizet Mar 23 '13 at 22:32
  • I did eventually figure this out somewhat with the help with your code. – user2202767 Mar 24 '13 at 01:40