I have written many FORTRAN codes, but am new to Java, which I find much more powerful and more difficult.
I have a program that was written several years ago in MS Access.
The program consists of a main window with buttons that take you to other windows where you input data and make queries.
Things have changed and the program needs to be rewritten. I want to rewrite it in Java.
I have written many small programs in Java, but they just do engineering calculations and only involve one window or screen.
To write Java programs, I have been using netbeans 5.5 and can easily design guis and add functionality.
However, I cannot figure out how to code multiple windows that interact with each other. For example, I would like to do the following:
1. Add a button1 to JFrame1, which will be the main screen-size window (I think they call it the Parent)
2. Add a button2 to JFrame2, which will be a secondary screen-size window (I think they call it a Child).
3. Add functionality so that when button1 is clicked, JFrame2 is displayed instead of JFrame1.
4. When button2 is clicked, JFrame1 is displayed again. Can you suggest how I can do this? I don't see it covered in the three Java books I have been using.
Can supply a simple Java code that does this or direct me to a reference that explains how to do write the code.
Thank you.
Recommended Answers
Jump to PostAs long as the two frames have references to one another, you can easily interact between the two. Another idea may be to use a non-visual controller that can act as a mediator between the two frames and synchronize interactions and state changes as needed.
Jump to PostHere is a small example with a view controller. The two view classes defined have essentially the same code here (which you would never want to code into a real app), but since they just represent your other two frames I made them separate for clarity of the example.
Jump to PostThe HashMap just provides a mechanism to look up a frame reference by name (the string key assigned when it was registered). The showView() method just loops through all of the views in the map and sets all but the requested one to not be visible.
This was just …
Jump to PostYes definitely. I only included all of those as static inner classes for compactness of the example. Each of those would normally be an independent class on it's own. You will noticed that I used them as such in the main() method.
Jump to Postand oh, get into the habbit of always using packages for everything.
It may not be required by the language spec (an omission which by many is considered a major historical flaw in the language) but it's good practice and many application frameworks and libraries expect all classes to be …
All 19 Replies
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.