Java MVC multiple JFrame - java

I am creating an mvc java application with netbeans. first, when running the program, it should be only show 2 buttons "Teacher" & "Student".
when I click the teacher button, it will leads me to viewTeacher where i can input or edit data about "Teacher". same goes for student button.
I have finished the whole MVC but I am confused about the first buttons:
- should I make more 1 view?
viewForm, viewTeacher, viewStudent.
viewForm is the first 2 buttons appear: "Teacher" & "Student"
OR
- should i put all the codes inside viewForm and create viewTeacher & viewStudent form on other components?

Instead of using multiple Jframes consider using multiple jPanels.
Say you have a MainPanel in a JFrame. The main panel will have a view panel and a control panel. The control panel at the bottom and view panel above it. When clicked on a particular button add the corresponding panel say, teacher panel to the view panel. You can create the teacher panel and student panel at the start but show them only when clicked.
Writing every thing in one class will make the code complex.

Related

How can I make two or more panels in Java Swing/GUI visible at once without having to press a button?

I am designing a game for a project in Java Swing using a JForm. I know that you have to use the code
jPanel1.setVisible(true);
I used this for all panels, even in the init() method in the source.
I am using the "Layered Panel" option for every panel I add. I placed the image on the first panel:
There are radio buttons in my second panel. I want the user to click on the option while looking at the picture, kind of like clicking on an icon (radio button) on desktop (background image).
Like this:
When I try to look at my design preview, it just shows me Panel2 (only radio buttons), not 1.
Is it possible to fix this (make two panels visible at once), or should I just make a button to switch between the panels?

Open elements and struct of jFrame in another like a menu

i have 5 jFrames in my java project. And i want to make like a Main Menu.
I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame.
And if i click other button the main frame will clean and charge other jframe.
It is possible? im programming with java jdk 8 and netbeans.
Thanks
Edit:
I think who marked duplicate didn't understand my question. I don't want to open or close the frame, or other frames, I want to load the structure and components of several in the same frame. Please read my question before you start complain that is duplicated
i have 5 jFrames in my java project.
And that's a problem.
And i want to make like a Main Menu. I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame. And if i click other button the main frame will clean and charge other jframe.
Yes this can be solved by getting the contentPane (usually a JPanel) from the JFrame whose content you want to display within the currently displayed JFrame, and making it the contentPane of the displayed JFrame, for example:
// create the new JFrame, the one whose content you wish to display
NewJFrame newJFrame = new NewJFrame();
// get its contentPane
Container newContentPane = newJFrame.getContentPane();
// add this content pane into the displayed JFrame
displayedJFrame.setContentPane(newContentPane);
// revalidate and repaint the JFrame so that its new data is well displayed
displayedJFrame.revalidate();
displayedJFrame.repaint();
// displayedJFrame.pack(); // and you might need to do this if sizes are way off
But this extra gymnastics is bulky, prone to bugs and unnecessary. You are painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
For this situation what I recommend is that you do that, that your GUI classes create JPanels, and that you add the ones that you want to swap to a JPanel that uses a CardLayout. And then whenever you want to show a different "card", call show(...) on the CardLayout object, passing in the JPanel that uses it, as well as the String key that was used when adding the "card" JPanel to the CardLayout-using JPanel. This is all well-explained in the CardLayout Tutorials.
Other useful links:
For rationale on why to avoid manually swapping please see: What's so special about CardLayout vs manual adding/removal of JPanels?
For using a CardLayout to help control a "multi-page" application with multiple classes, please see: How to Integrate Multi-page Java Desktop Application from Multiple GUI Classes

Automatically populating Components in NetBeans

I am working on my Final Year Project and have been stuck in a problem for last 1 month and it is giving me sleepless nights. It's a Test Creating and Online Test taking application. Each Question is stored in Data Base. How can i automatically populate different Net Beans properties according to the type of the Question. for example Combo boxes for each MCQs and Radio Buttons for T/F Type Questions.I mean Automatically creation and Population of components.
I would add a JPanel to your design that is the correct size for components you want to change dynamically. Select the JPanel and right-click for a pop-up context menu. Select Set Layout -> CardLayout. Now when you drag components onto that JPanel instead of showing them in different positions they will just stack on top of each other. You can see the list of components you have added in the Navigator tree. You will only be able to see the top one in the design itself. Select each in the Navigator and edit its property for Card Name in the Layout section near the bottom. The name is probably card1 or card2 etc. Change it to something you can remember for when you write the code below.
Then you would put code like this whenever you need to change the displayed component.
CardLayout cl = (CardLayout) this.jPanel1.getLayout();
cl.show(jPanel1, "checkboxcard");
"checkboxcard" was the name previously set as the Card Name property.
The embedded components do not have to be simple swing controls. You could create several JPanel designs and drag them into the parent jpanel with the cardlayout. For each internal panel, select your project and select New -> JPanel Form to create a new panel you can design. Compile the corresponding class and then drag it from the Project tab to the design with the parent jpanel.

show one panel after button is clicked and other panel when second button is clicked in the same frame

I am creating a Swing based application , which actually consist of two buttons as shown below -
now what i want is when first button is clicked it must perform a action like showing a panel containing label, textfields , and some buttons in the same frame as shown below -
and when second button is clicked it will show another panel in the same frame as shown below ..
the thing is i am not understanding how to make this interface in action by providing event handler and action listener .. So please letme me know how can i archive this . Any help would be appreciated .. Thanks
There are 2 approaches.
CardLayout based.
Create all the panels (empty, panel with fields, panel with list) and add them in a container with CardLayout (the empty one is default). On click buttons swap visible cards (panels) showing necessary one.
Recreation based.
On click button create a new panel with new content. Remove the old one from container and add the newly created pane. After then call:
container.revalidate();
container.repaint();

Assigning a different (new) JPanel to a JTabbedPane at run time

I have a JTabbedPane (say myTabPane) having one tab (lets take only one tab for clarity sake). While creating the JTabbedPane, I added a JPanel (say panel_A) to this tab. I have a button on this JPanel. The tab displays my JPanel perfectly with the button on it. So far so good.
I have defined a listener on the button which creates an instance (say panel_B) of another class extending JPanel. This JPanel has got a different set of components on it. I want panel_B to super-impose panel_A. That is, JTabbedPane's tab should show panel_B and hide panel_A.
Please note that I am able to display panel_A OR panel_B when I "bind" the respective panel (one of them) to the tab during creation of the JTabbedPane. However, I want a selective display (or binding, whichever is possible) of only one of the panels with a button-click (ie. at runtime).
How can this be achieved?
Thank you!
This will do what you need:
myTabPane.removeTabAt(0);
myTabPane.addTab("B", panelB);

Categories

Resources