AUTOMATICALLY FILL IN SECOND JFRAME - java

I have a question, im making a application where you can first select a name from a jList in a jFrame. After you've selected a name and pressed the proceed button, a second jFrame pops up.
On this jFrame there are a couple of textfield which i want to automatically fill up with information about the selected name selected in the first jFrame.
And this information which is automatically filled in is different depending on the chosen name.
I already have the first jFrame with 4 names in a arraylist but now im stuck on the second part where the chosen name is transfered to the second frame along with the extra information which must be done automatically
I hope someone can help me, i would really appreciate it.
THank you.

Well, heres how I would do it:
Exit the current jframe
Pass the data in the arrayList to a function that creates a new JFrame
Fill the new JFrame

Creating a a Swing application with multiple JFrames can be problematic.
A better and simpler approach would be to use CardLayout and to have panels within a single frame. Your ArrayList could be a member variable in the main JFrame class and could be easily be updated with data as you progress.
See: How to Use CardLayout

Related

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.

Creating a frame class in order to make many InternalFrames

BACKGROUND
I'm creating a hostel booking software as a project to learn java. I have my main frame which holds buttons that open each room by setting the containing frame to visible. I have an add room button that asks me how many beds the room as and stores this as an int.
QUESTION
Would it be possible to have a room class that would allow me to create new rooms by pressing the button? That means creating an InternalFrame, JPanel and JButtons, adding the buttons to a grid based off the bed number int.
I feel like the idea is there but the implementation is proving tricky since im new to java. Any ideas are appreciated, thanks!
Read the section from the Swing tutorial on How to Use Internal Frames.
The demo in the tutorial shows how to dynamically create internal frames when clicking in a menu item. The concept is the same for buttons. You just add the ActionListener to the button.
Of course you will need to change the code to create your own custom panel. You will also probably want to use a JOptionPane to prompt the user for the number of rooms. Again the tutorial has a section on How to Use Dialogs for an example of using a JOptionPane.

Display multiple jPanel based on a variable

I'm stuck on my project and I have 2 days left so I hope you guys can help me.
My JFrame "UserManager" displays the user's informations which are:
Name,address,phone number etc
BankAccounts
My problem comes when I have to display the BankAccount objects: since every user can have an unlimited number of bank accounts - each one stored in an Arraylist called "ownedAccounts" - I want to create small panels inside my JFrame.
Instantiating the jPanels isn't a big problem: I thought I could create a class called "BankAccount Panel" and then do something like this in my JFrame:
for(BankAccount b:thisUser.ownedAccounts){
BankAccountPanel newpanel;
}
Or something like that.
The fact is that I'm using the NetBeans GUI Builder and I have no idea how I can place these BankAccountPanels on my JFrame and how to dynamically resize the JFrame itself (I may need more space to show every BankAccount)
Of course each panel has to be placed under the previous one.
You can create new panels (or derived panels of your desire) programmaticaly and add them in existing component.
I suggest creating one parent panel with layout of your choice and adding each new panel into it with .addcomponent() method.
If the number of accounts is not limited, I suggest using Scroll Pane.

Replace All JFrame Icons Globally

I can replace any given JFrame icon in Swing with a call to JFrame.setIconImage(). However, I have an existing application that pops up lots of JOptionPanes and other JFrames/dialogs, and rather than track them all down I'm wondering if there is a way to switch these over to my custom icon all in one place? Also, in some instances (ProgressMonitor, for example) I don't have access to the actual JFrame to fiddle with.
You can store the JFrames you created on a List. Then, you can use a loop to get every frame on this list, and apply the icon.

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