Netbeans GUI Builder - java

I have a question about Netbeans GUI Editor.
I created this test application containing a MenuBar using the GUI Editor. However I need to Add another swing component (Let say a Jbutton) to the frame that contains the MenuBar directly from the code and not using the GUI Editor.
I Succeeded in adding the Jbutton but I found that the only way to do it is to change the layout from the code. The result was not what I expected since the menuBar added by the GUI builder changed it layout also.
So briefly I want a way to add The Jbutton from the code without affecting the original components added using the GUI Builder.

The GUI builder uses the Group layout. if you want to preserve the layout of the original components you need to stick to this layout. Codeing by hand in this layout is awkward but the following link will help.
http://docs.oracle.com/javase/tutorial/uiswing/layout/groupExample.html
You'll need to add your button, using the addCompenent method, to groups in both the horizontal group and the vertical group.
Good luck!

Related

How to add combobox from source code along with drag and drop GUI Java NetBean

I'm starting using java with NetBeans IDE. I'm using drag and dop GUI, it's so easy to use, but I got a problem. I'm writing this code at the contructor:
JComboBox combobox=new JComboBox();
combobox.addItem("Apple");
combobox.addItem("Banana");
for(int i=1;i<=10;i++){
combobox.addItem(i);
}
just right above initComponents(); hoping that my new combobox will shown when I run the project, but it doesn't. Did I do something wrong? Thanks in advance
Yes, you are creating a JComboBox, and yes, you are adding items to it (numeric and int -- that's a problem, but that's a discussion for another day), but no, you're not showing any code where you add this newly created JComboBox to a component that is displayed in the GUI. To display a component in a Swing GUI, it must be created and added to a component that is ultimately displayed in a top-level window, in the "GUI".
So this begs the question, how do you add your created JComboBox to your GUI that you've created with drag and drop code? One way: you could add it to say a JPanel that's already in your GUI, but you will need to do this after initializing components, usually this means after the constructor calls initComponents(), and you'll also need to make sure that this JPanel uses a layout manager that makes it easy for it to accept new components (this means most of the layout managers except NetBean's default layout, GroupLayout).
There are other issues, such as whether or not the container holding your JComboBox is large enough to display it, but the best suggestion that I can give is for you to go through the Swing tutorials, and hit especially hard the layout manager section. You can find links to the Swing tutorials and other Swing resources here: Swing Info.

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.

Revamp Panel Based UI to JTabbed Panes

I have developed a simple music player in Java which can play any given playlist or simple mp3 song.
For now i have worked all things out in plain JPanels. GUI doesn't look neat.
I need to revamp the GUI using tabbed pane. How this can be achieved using existing JPanels without affecting current functionality?
Also, i am not able to figure out shall i go for Tabbed Pane or Card Layout?
http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
Because a tabbed pane provides its own GUI, using a tabbed pane is simpler than using the CardLayout class.
"Also, i am not able to figure out shall i go for Tabbed Pane or Card Layout?"
It really depends on your preference of the look of your program. The two layouts perform very similarly, though CardLayout is a little bit more code, though at all not difficult. If you don't want the tab look, which I don't see why you would, for a game, then go with CardLayout
"How this can be achieved using existing JPanels without affecting current functionality? "
You need to create separate JPanel for each containment of whatever components you want in each tab. Then just add those JPanel to the JTabbedPane. It shouldn't break any functionality, just the look. Components from another panel should not be affected, you just won't be able to see any changes made, unless that other panel is in view.
If you want to go with CardLayout you can look at the tutorial

Eclipse Visual Editor: Components not shown in JTabbedPane

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.

Categories

Resources