Should I create a new window or modify the old one? - java

I am programming a GUI application in Java. I do it for the first time.
I would like to have a form (with radio buttons and so on). After the form is filled in and the "Submit" button is pressed I would like to have a new window. I see two potential ways to do it:
Close the "old" window and open a "new" one.
Remove "old" elements from the existing window and put there "new" elements.
What is the standard way to go? If it is the first way, what is the command to close the window? If it is the second one, how can I remove elements from the existing window?

What you should do is create new JPanel for all the windows you want to show, then remove (or hide) the panel you want to hide and add or show the one you want to show.

I don't know too much about Java so I can't answer to your specific questions, but I want to remind you of the window opening/closing effect since Windows Vista: It looks kind of weird in some older setup wizards where everytime you click next the window fades out and in...

I think the most logical way is to have 2 objects("Close the "old" window and open a "new" one")
Anyway, I suggest you make an abstract class with the common elements, and then extend it with Window1 and Window2.

Java frames are destroyed with the dispose() method.

Related

Java GUI Design view not showing completely but code works

I am new to Java GUI I deigned and window and menu item using java design tool.But when I want to create window for menu item say New contact I did not find an option to do that in event handler so I did it manually by coding it.But when I go to design part and click on New Contact it does not show the window I created via code.
Here is the screen shot of deign view -when I click on New Contact nothing happens.
Now in the source code when I run it I get the window I coded
Is there any possible way I can make it work in design part? I did not find any option to do it in Add Event Handler
You do not want a JFrame to show another JFrame -- that's a bad GUI design since it means that your application is actually two applications. Better to show a dialog window such as a JDialog. Please see The Use of Multiple JFrames, Good/Bad Practice?
If you want to design a 2nd window, create a new Java program in NetBeans, one that creates this second window (again, better for it to be a JDialog, not a JPanel)
Give it a constructor that allows passing in the parent window, and then pass that to the JDialog super constructor
And then in your ActionListener code above, create a new object of this new program, passing in the current JFrame.
In the future, please post code as code-formatted text, not as an image, since this way we can copy, paste, compile and run it if we want, allowing us to better understand your code and your problem.

How to create dynamic wizard?

I am making project with GUI. The thing is, that I have a button and what I need to do is that after clicking this button I need to change Frame layout. For example, like when you are installing some program and you click "next" button, the Frame layout changes and you can see some different content. Basicly, dynamic wizard.
I have tried use another Frame, but it opens in another window and that is not what I want. I want to open it in the same window.
Another thing I have tried is set visibility of these components I don't want to be displayed to false, but I find it unprofessional and it is overlook in making a desing, when I have components over themselfs.
So do you guys have any idea? Thank you.
Most of the times for a wizard like GUI, you should have JFrame and a set of JPanels. In each step you can pass the shared data as constructor arguments to each panel, and when you are making one of them invisible and make another one visible, you can get some date from the previous step panel and pass it to the next step panel(if needed).
It is very common that your panels extend the JPanel and have some argument in their constructor(s). You use these data for initializing your panel and managing the state of the overall progress.
There is no a total plan for all situations. So you should decide what to do which is best fit for your case.
Try not to have multiple JFrames.
Hope this would be helpful.

More pages, same window/frame. Eclipse Windowbuilder

So I have this project to make and I'm somehow stuck. I have an encyclopedia to make and I do not know how to make the pages.
For example, I have a JFrame with two buttons, called Back and Next. When I press that button, I want my program to switch to the next page, same as an e-book.
I thought of creating a new JPanel, get the X and the Y of the first frame and close the first frame when this one opens, reopen it only when I press back. How can I do that?
You have several options:
Create new JPanels for each "page" and swap them via a CardLayout.
Create a single JPanel for displaying page information, and then swap content on pressing the button. I favor this solution if at all possible as the simplest and the one best suited to a good MVC solution.
As an aside, if you're new to the Swing GUI library, then I suggest that you put the GUI-builder to the side for a bit til you learn the underpinnings of the library. This will help prevent you from painting yourself in a corner should you use the GUI builder later.

How are GUIs usually manipulated?

I am attempting to learn Java (at this point GUI programming in Swing and as a concept in general) and I have managed to create a basic login page. What I want to do however is to have it change what is displayed somehow to a 'home' page, as it were. I can think of only two ways of doing this, the first being opening a new window and closing the old one and the second being somehow changing the frame the login page is in to whatever I want to display. How is this usually done in real-world applications?
There are a number of ways that it can be done including the two that you mentioned. It really depends on what you are trying to achieve. Something like a login form could be done with a JDialog that pops up over the main window such that you start the main window and the main window simply pops open a JDialog for credentials etc.
Sometimes you create multi document interfaces using JInternalFrame. Where the various windows that you need all appear to be within a bigger main window.
So if you were creating a simple UI then simply switching out the content of the JFrame as you suggested would suffice. You can use an apprpriate LayoutManager to assist such as a CardLayout.
If you have a login frame, a better way is to make it a modal dialog, which is:
1) more user-friendly.
2) making your coding job easier.

How to use setVisible in JFrames?

In my program I have two JFrame instances. When I click next button I want to show next frame and hide current frame. So I use this.setVisible(false) and new Next().setVisible(true). But in Next window if I click back button I want to set previous frame to be visible again and next frame must be ended (which means it must be exited).
Is there any special method(s) to do this? How can I do it?
Consider using CardLayout instead of hunting for how many JFrames there are. Then..
only one JFrame would be needed
any of Next/Back Actions will be only switching between cards
There are lots of examples in this forum - e.g. as shown here.
That is an odd & quirky GUI. I suggest instead to run a JFrame for the main GUI, and when the user wants to search, pop a JOptionPane (or modal JDialog) to accept the details to search for. This will not have the effect described above, but will follow the 'path of least surprise' for the end user.
If you want to destroy a JFrame releasing all associated resources you shold call dispose() method on it.
You may place your JFrames on a list data structure and keep a reference to current position according to the window you are displaying. In that way it will be easy to move to next and previous. But note that each frame added to the list will use memory and will have its state as you placed it in to the list.
If you are trying to create a wizard like UI, you should look up Sun(oracle)tutorial here.
create the instance of your main window in next() window.. and use same method which you chosed befoe to hide your main window, for example if your main window is named as gui then what we have to do is.
gui obj = new gui();
and if you click on back button now than do these also
this.setVisibility(false);
obj.setVisibility(true);
that's all you need.
good luck.

Categories

Resources