0

I am making a Java gui project and it consists of two frames.

The problem is that when I call the secondframe from the firstframe, I have set it such that the firstframe visibility is set to false. The problem is how do I make the firstframe visible again by using a button from the second frame.

should i ditch this method and create a new jpanel instead??? Does jpanel have similar capabilities as jframe?

chettyharish
  • 1,704
  • 7
  • 27
  • 41
  • 4
    Have a look at [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) – Guillaume Polet Feb 02 '13 at 15:47

1 Answers1

4

Consider using CardLayout. This way you can switch via multiple UIs without needing another frame. Here's how to use it.

Edit: As Guillaume posted in his comment, this answer from Andrew also covers how to use the layout.

Edit2:
As you requested a little more information about my latest post, here's how such a class may look like:

import javax.swing.JFrame;


public abstract class MyFrameManager {
    static private JFrame   startFrame,
                        anotherFrame,
                        justAnotherFrame;

static public synchronized JFrame getStartFrame()
{
    if(startFrame == null)
    {
        //frame isnt initialized, lets do it
        startFrame = new JFrame();
        startFrame.setSize(42, 42);
        //...
    }

    return startFrame;
}

static public synchronized JFrame getAnotherFrame()
{
    if(anotherFrame == null)
    {
        //same as above, init it
    }

    return anotherFrame;
}

static public synchronized JFrame getJustAnotherFrame()
{
    //same again
    return justAnotherFrame;
}

public static void main(String[] args) {
    //let's test!

    JFrame start = MyFrameManager.getStartFrame();
        start.setVisible(true);

    //want another window
    JFrame another = MyFrameManager.getAnotherFrame();
        another.setVisible(true);

    //oh, doenst want start anymore
    start.setVisible(false);
}
}

This way you would only instantiate every JFrame once, but you could always access them via your manager class. What you do with them after that is your decision.
I also just made it thread-safe, which is crucial for singletons.

Community
  • 1
  • 1
Zhedar
  • 3,480
  • 1
  • 21
  • 44
  • `JTabbedPane` can also be used to remove the use of multiple frames – Guillaume Polet Feb 02 '13 at 15:51
  • That's right, but that would let the user always switch behind those frames. Maybe the questioner doesn't want that. – Zhedar Feb 02 '13 at 15:53
  • Actually one of my frames calls like 10 other frames(which I have not created yet) and the other frames themselves call other frames. So tabbed pane are useless. – chettyharish Feb 02 '13 at 16:05
  • @chettyharish So `CardLayout` would be the best way to go in this case. You could also use a `JDesktopPane` with `JInternalFrame`s if you want to make multiple frames usable at the same time, but MDIs are quite yesterday imo ;) – Zhedar Feb 02 '13 at 16:10
  • but lets say is there a way by which you can set visibility of jframe from another jframe? – chettyharish Feb 02 '13 at 16:11
  • You could build yourself a WindowManager class which you could use to set visibility of your frames. With this factory you could instantiate your frames just a single time(Singleton pattern) if that's what you want. But multiple frames for a single application could make your user go crazy ;) – Zhedar Feb 02 '13 at 16:15
  • any tips please as i am new to this. Any specific links, examples or maybe tutorial? – chettyharish Feb 02 '13 at 16:26
  • edited my answer with a simple way how you could implement that behavior using creation design patters. hope that helps. – Zhedar Feb 02 '13 at 16:40