Swing containers within containers - java

I am somewhat new to the whole java swing scene, and I just want some clarifications due to a slight confusion I have. I have learned about orientation and buttons and all the basics. Also pointing to some good (non oracle) tutorials will be highly appreciated.
As far as I understand, we have our JFrame which is a window.
Then our JFrame consists of ContentPane, which I am using a container for.
Container content = frame.getContentPane();
Now that I have this container, can I add more containers within those containers? Let's say that I would like to have different parts that do different things, and for that I would like to create classes and such that each handle their own containers?
So what I am asking for is, how does one go about storing different content within the container? What is the proper way to go about it?
An example I would give is let's say I have a scoreboard (for soccer) that is on the top part of the window, on the middle part of the window there is some work related business stuff, and on the bottom part of the window I have some textbox that does its thing with a few buttons.
Sorry if this question is stupid, I am just trying to learning swing, and want to know proper way to arrange different components within the window.

Yes, you can. Create an instance of JPanel and add your components to it, and then add them to the frame's content pane using a string:
JPanel panel = new JPanel();
//code to add stuff to the panel
frame.getContentPane().add("Center", panel); //"North", "South", "East", "West", or "Center"

Related

Adding new objects to a jframe

I have seen a program and was hoping to replicate one feature of it in my program. The program is a flashcard app, and when you create a new deck it adds another section to the homescreeen. I will attach images. I would love some advice on how to do this.
One of possible way is to use correct layouts, here is examples. Using this you may dynamically add any components (labels, images, buttons etc.) into your JPanel (I recommend to use JPanel as main container of all components instead of JFrame that should be one and just contain your JPanel).

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

issue about Concept of JFrame, JLabel and ContentPane

I just study window programming with awt.
I see through several codes but I can not get concepts of JFrame, JLabel and ContentPane.
I think JFrame only make outer Frame.
ContentPane is container that contain JLabel that has contents(text, button, radio etc...).
I don't know this is correct T.T
Why I ask this is I failed combine the contents.
I can not make TextField and InternalFrame at a time.
I want to know the concept.
I hope you take my question right.
You need to get clean view for AWT vs Swing. Here is a good explanation for Swing or AWT: Which is right for you?
JFame : An extended version of java.awt.Frame that adds support for the JFC/Swing component architecture. See How to Make Frames
JLabel : Display component for your short text like Name : , Phone Number : etc., See How to Use Labels
Container : Container is a component that holds or wraps-up other components. It aids with grouping related components together in the GUI. Document of Swing Containers

Constant dialog-like functionality on a particular portion of a screen in a Java Swing application - is JDialog functionality the way to go?

To expand upon the headline :
I have a screen (my main window, an encapsulated JFrame) that's going to be created most likely with a GridBagLayout, because I need a grid whose cells are to be differently-sized rectangles. In one of these rectangles will be a malleable dialog-like functionality, with different options depending on the context of the application.
My question is, are custom JDialogs the way to go here? Or do I simply want a reusable JPanel that has the particular buttons I want displayed or disabled depending on the context? I hope this is clear; thanks. -B.
Go with the JPanel solution.
JDialog is a heavy-weight, top-level container, meaning it's window is managed by the system and cannot be embedded as a child of another component.

Categories

Resources