Creating a frame class in order to make many InternalFrames - java

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.

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.

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.

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.

AUTOMATICALLY FILL IN SECOND JFRAME

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

Trouble with Java GUI

I am creating a GUI for my project. I am completely stuck at one point. I was able to create a GUI which will make some simple queries like the release number, command name, and few other boolean questions.
Now I want the user to press CONTINUE button to move to the next page of the GUI form. I have created the continue button, but now I need to register an event to it. This is where I am stuck. I have no idea what event can I register to it which will move the GUI to next page. (By moving to the next page I mean, the different GUI pages we see when we are installing a software for our computer.)
For instance, if I am installing iTunes, I'd first select the radio button for "I accept the terms and conditions" and then I'd press the CONTINUE or the NEXT button to move ahead. If I need to come back, I'd press the BACK button.
One logical answer would be to create another GUI form and then link it to the one I created first.
EDIT:: this is the first time I am working in Java, so I might have ignored some obvious facts.
Look into using a CardLayout, adding several JPanels to the CardLayout-using container, and then to swap to the next view (the next JPanel), call the layout's next(...) method in the JButton's ActionListener. You could also randomly access components held by the CardLayout using its show(...) method.
To learn more about this including sample code, please have a look at the CardLayout Tutorial, and the API.
Well, register an ActionListener or an Action with the button. To do that wizard style, have a look at the CardLayout layout manager to switch cards or use a tabbed pane, hide the tabs and switch them inside the action or action listener.
if i understood, i think you should use the CardLayout
If you are using Swing, you can using several instances of JPanel over a one instance of JFrame.
You can have something like that structure:
JFrame
+------JPanel:root
|
+---JPanel:current // This panel change by other instance
|
+---JPanel:controlPanel // This panel contains you button
In you button need add a ActionListener with the method addActionListener
The panel control may need changes your elements, may the text of button or remove the listener and change by other.
I hope that can help

Categories

Resources