0

I'm trying to be creative with a class project in Java.

I have a JFrame which takes in some input from the user, and I want to create a sort of pop up frame that will show a graph from the input.

The second frame is only going to be showing a graph. I'm having trouble realizing this.

I'm using the GUI builder(Netbeans) for the main JFrame and at first I tried to just create an empty JPanel and set it visible from the button, but that didn't work and I soon found out I couldn't.

I had to use a JFrame or some other container apparently(am I wrong?).

So now I'm thinking, how will I pass the information to the second pane for the graphing information?

Anyways, I'm just looking for some input or good practices in order to accomplish this. I'm reading up on CardLayout now but that's not the solution I want since it would require me to create a pane to hold the graph component. I just want the graph button to open up a graph pane(or frame) and close when the user wants to. The main frame is only used to take the input for the graph. Thank you for any input.

There will be a question soon on how I can actually graph something in Java, but ill tackle one problem at a time

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
gandolf
  • 3,384
  • 4
  • 21
  • 20
  • 3
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) It seems you are after a `JDialog` or `JOptionPane`. – Andrew Thompson Feb 19 '13 at 05:14
  • @AndrewThompson, I have seen talks about using Dialogs, but I am new to the internalJFrames, are there any differences between the two that would affect the decision to use either? In the link you provided I can see clearly now that multiple JFrames is frowned upon so I will avoid the headaches, but as far as being able to keep the data inputted consistent and easy to handle for the graph Pane, would there be any advantages of using dialogs over InternalFrames?. Thank you. – gandolf Feb 19 '13 at 05:53
  • If there are potentially multiple graphs, then a `JDesktopPane` with `JInternalFrame` children for the graphs might be good, or alternately a `JTabbedPane` with one graph in each tab. For a single graph, a dialog would be fine. There are lots of possibilities. – Andrew Thompson Feb 19 '13 at 08:06

1 Answers1

0

You could create another class for the pop-up JFrame which would probably be the best idea. You could do something like:

public class PopupFrame extends JFrame{

    //Declare variable you wish to use for your graph here.

    public PopupFrame(/* Pass in variable(s) that you will need to display the information. */)
    {
        super("Graph");

        //Set the graph variable equal to the variable to passed into the constructor.

        add(/* Put in the variable that you just initialized above and that you declared outside of the constructor. */);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500,500);// Change to the dimension that you want.
        setLocation(50,50);// You could also have the constructor give it these values if you want to center it on your parent window.
        setVisible(true);
    }
}

and in your main class, have something like:

PopupFrame f = new PopupFrame(/* Graph variable(s) passed in here. */);
f.show();

Hopefully this was of some use to you. Good luck!

Holden Lewis
  • 387
  • 3
  • 18
  • Hi @lonnez, This is what I had in mind actually, by passing the data to the constructor, but reading other threads on the issue it would seem managing multiple JFrames was just a bad idea to begin with. But thank you for your snippet. – gandolf Feb 19 '13 at 05:54
  • 2
    A popup JFrame is NOT a good idea. An application generally will only ever have a single JFrame. For popups you would use a JDialog. – camickr Feb 19 '13 at 06:05