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.
Related
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
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.
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.
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);
I want to create a JTabbedPane in a Visual Class using the Visual Editor in Eclipse.
I selected JTabbedPane from the beans menu on the toolbar.
I next clicked on the class to drop it in. So far, no problems.
But wait, no tabs are showing up.
The tutorials on the web say to drop a component into the TabbedPane and the tabs will show up. I dropped a JLabel and no tabs showed up.
Deleted the JLabel using the design window. Dragged a JTextPane, still no tabs.
I drag a JTextfield, onto the TabbedPane (without deleting the JTextPane), still no tabs showing.
I set the "tabs_text" properties of the JTextField and the JTextPane. Still no tabs show up.
Also, the components are not showing on the TabbedPane. It's like a gray whole that just swallows things and no images are reflected back.
Anybody have a step by step tutorial, instructions or something similar for how to put a JTabbedPane onto a Visual Class and also components into the JTabbedPane? Pictures would be very helpful.
Do I have to use NetBeans or hand-code the JTabbedPane?
Note: I'm trying to create a tabbed pane (or notebook in other GUI terminology) with one tab for person's address and another tab for phone numbers.
You should be able to create a JTabbedPane with the editor. But since you asked:
Do I have to use NetBeans or hand-code the JTabbedPane?
I will answer that with no you don't. But I would very highly recommend hand-coding the entire GUI. Visual Builders are nice for prototypes but produce much pain and bloodshed for those who have to maintain the code. Once you get past the Swing learning curve, you will find that Builders only limit your ability.
The TabbedPanel is showing up. Here's what I did:
I created a separate class,
Residence, derived from JPanel. Added
all the labels and fields.
Modified the addTab statement to
add the new panel.
Changed the add method to use 2
parameters, not 4.
Removed all "ve" comments, placed in
the code by the Visual Editor.
I don't know which, any or if all of the above was necessary, but that is what I did and now the TabbedPane is showing in the Visual Editor.
Looks like the Visual Editor may be more useful to show what the form looks like rather than as a tool to build them.