-2

What I have: Two Classes that instantiate two JFrames.

What I am trying to achieve: One with a button and the other that will become invisible when a action is fired on the button.

Problems: I do not know how to pursue this. How should I go about coding this?

Class 1

public class test1{
    public static void main(String[] args){
        JFrame frame = new JFrame("Blinkly Frame"); 

        frame.setSize(100, 100); 
        frame.setVisible(true); 
    }
}

Class 2

public class test2{
    public static void main(String[] args){
         JButton button = new JButton();
        //when i will click this button i want to make invisible frame
    }
}
DarkV1
  • 1,776
  • 12
  • 17
Dima
  • 19
  • 1
  • 5
  • Your question seems a bit unclear. Please try to tell us more and show us more. – Hovercraft Full Of Eels Jun 06 '16 at 18:30
  • I have two pages,the name of one is test1.java and another test2.java. In test1 I have jframe in test2 jbutton and I want to make invisible this jframe which is in test1 with the button which is in test2 – Dima Jun 06 '16 at 18:35
  • It might be helpful to edit your question to include the code for each page. – Lexi Jun 06 '16 at 18:37
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 3) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jun 06 '16 at 18:41
  • You've got two main methods which will result in two JVM's running -- You don't want to do this, you only will want and need 1 main method. This suggests that you may not be clear of the basics of Java yet, making it hard for us to know how to advise you other than perhaps you will want to study a basic Java tutorial to learn the rudiments of the language. – Hovercraft Full Of Eels Jun 06 '16 at 18:50
  • i have only one main methods here I just copied from test1 then edited it but forgot to delete the main method from test2 – Dima Jun 06 '16 at 18:54

1 Answers1

1

Solution: Create an instance of the class that has a Jframe or extends a JFrame.

First we need the JFrame that will be disappearing.

public class BClass extends JFrame{ 
    // Disappearing frame
    public BClass()
    {
        this.setSize(300,300);//sets frame properties
        this.setLocationRelativeTo(null);
    }
}

Next we need the Frame that will be holding the buttons. (Documentation added)

public class ACLass {

public static void main(String[] args) {

    JFrame frame = new JFrame("Magician"); // instantiates 
    BClass b = new BClass(); // instantiates class that extends JFrame

    b.setVisible(true);//
    frame.setSize(300,300);//
    frame.setVisible(true);//
    frame.setLocationRelativeTo(null);//

    JButton disappearButton = new JButton("Disappear"); //Adds button
    disappearButton.addActionListener(new ActionListener() { // Adds action -When button is "clicked"

        public void actionPerformed(ActionEvent e) { // method called when action fired
            b.setVisible(false); //visibility changed
        }
    });
    disappearButton.setBounds(0,0,300,150);

    JButton appearButton = new JButton("appear"); //Adds button
    appearButton.addActionListener(new ActionListener() { // Adds action -When button is "clicked"

        public void actionPerformed(ActionEvent e) { // method called when action fired
            b.setVisible(true); //visibility changed
        }
    });
    disappearButton.setBounds(0,100,300,150);

    frame.add(disappearButton, BorderLayout.PAGE_START); //adds button to frame
    frame.add(appearButton, BorderLayout.PAGE_END); //adds button to frame
 //I used border layout however use the a layout manager that works with your components/frame

}

}

Exotic Explanation

I'm going to explain this in terms of a magic show as it provides better understanding. So first, we have one magician(JFrame) and his wand(JButton) and then we have the helper(the second JFrame) that will disappear and a stage that has been set(all properties defined etc.)

First the magician adds some magic power to his wand(actionListener that handles the button being pushed) that will react when the magician waves it(action fired a.k.a button being pushed).

Next we show the audience the helper(instantiating JFrame to disappear). As we have shown the audience the helper, we can now show him/her disappearing (Now we can call setVisible through the instance variable of the class).

Finally, the magician waves his wand(firing an action), and the helper gets the signal(actionPerformed method) to disappear. The helper then can call the

b.setVisible(false); //making the frame invisible

General Explanation

https://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html

Basically, by instantiating an object in another class you can also call the objects methods in that class i.e. setVisible(boolean b).

Other less preferable solutions: If your disappearing class has does not extend a JFrame but instantiates one.. You would need to create an instance variable

private JFrame j; 

Then use getters/setters to access the object which will then allow you to call its methods.

secondClass.getFrame().setVisible(true);//getFrame returns the JFrame

Then add that to the actionPerformed method.

You can also use a static instance variable and statically reference it in the actionPerformed method... (not recommended)

secondClass.frame.setVisible(true);
DarkV1
  • 1,776
  • 12
  • 17