0

So I've just started working with the Netbeans GUI editor, and I have a program that needs to switch between multiple frames, for organizational purposes in a group project I want each frame to be a completely separate class. What code do I need to put into a button to switch between frames? Only thing I've found so far is:

new jFrame1().dispose();
new jFrame2().setVisible(true);

But, I can not pass information from one JFrame to another.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

0

to pass information between them you can use Observer pattern . or use another class, and make static fields on it .

Gladiator
  • 264
  • 1
  • 5
0

Take a look at the MVC (Model View Controller) Pattern. There you have a model, a simple Pojo that contains the Data that is shown (view) on the GUI. You could pass the Model from one Frame to another.

Just an Idea:

To pass the Model from one Frame to another, you could use the Multiton-Pattern to create a Registry that holds Controller. And each Controller holds the Model, for which the controller is responsible.

So in each of your JFrames you would do something like that:

ControllerRegistry.getController("ControllerName").getModel()

to get the Model.
Your Controller could look like this:

@Controller(name = "ControllerName")
class MyController {
    private MyModel model;

    public MyModel getModel(){...};
}

The ControllerRegistry would then try to find all Classes on the Classpath that have the Annotation Controller. Then it would try to find the CoOntroller with the name from the annotation. The ControllerRegistry.getController()-Method would be static, so that you can access it everywhere without an Instance. The Registry could also cache the already known Controller to prevent searching it in every call of getController().

treeno
  • 2,510
  • 1
  • 20
  • 36