Insert a Frame into the component hierachy of another frame - java

For some reasons (reparent a Xwindow on a Java Application) I need to add a frame into another frame... But it seems it's not possible.
Is there any mean to insert a frame (in fact a heavy weight component) into the component hierarchy of another frame?

Related

Container (AWT) Object in Java

I am researching about AWT as Frame, JFrame and I saw object Contianer as parent of them but I wonder that can I use container object as Frame or JFrame, below is my code but it's not working:
public class icontainer {
public static void main(String[] args) {
Container icon= new Container(); // new JFRAME();
icon.setSize(300,300);
icon.setLocation(300, 300);
icon.setVisible(true);
}
}
Why don't we use icon = new Container() instead JFrame?
That is not how inheritance works.
A Container is a base class that does not much more but keeping track of its children. And that is it. Its not a window itself.
From the Javadoc:
A generic Abstract Window Toolkit(AWT) container object is a component that can contain other AWT components.
Components added to a container are tracked in a list. The order of the list will define the components' front-to-back stacking order within the container. If no index is specified when adding a component to a container, it will be added to the end of the list (and hence to the bottom of the stacking order).
But that is it. There is no mention of a window anywhere. It doesn't display anything on its own. It is displayed in something else.
A JFFrame on the other hand is a window (it literally inherits from java.awt.Window via java.awt.Frame).
From the Javadoc of java.awt.Frame:
A Frame is a top-level window with a title and a border.
And then there are some details about how the Frame layouts the contents etc.pp, and how the window decorations should look like and behave, especially wrt. WindowEvents like WINDOW_CLOSING. Those are things that are all only present in Frames, not Containers.
And if you go down to JFrame, you get some specialized handling of e.g. the content pane to make it work better with the JFC/Swing component architecture.
Container window = new Container();
This is code you can write, but nearly useless. In order for the container to actually be displayed on screen, it still needs to be put into a window.
Container window = new JFrame();
You can do that, because JFrame is a subclass of container, and thus is assignment-compatible to Container.
However, you lose the ability to call any methods introduced by java.awt.Window, java.awt.Frame or javax.swing.JFrame on the container -- because those don't exist on Container.
You should use a JFrame or Frame, because a Container of its own is not a "Window" that is displayed to the User.
I do not recommend it, but you could store your JFrame or Frame as a Container like this:
Container icon = new JFrame();
icon.setSize(300,300);
icon.setLocation(300, 300);
icon.setVisible(true);

Do I need JPanel always?

I am now writing code simple GUI that's for start the game window. I only need Do you want to start game message and start button on the window. But I have a confusing concepts for the JFrame and JPanel. Actually, I thought I need to add JPanel to JFrame to add the other components such as JLabel, JButton,...etc. But I realized I don't actually need JPanel. I can just add the components simply use add(button), add(label) to JFrame. So why I need JPanel. And I think JFrame doesn't need JPanel but JPanel need JFrame. Am I understand correctly?
No, not always. A simple graphical user interface may be implemented by just adding components "directly" to a JFrame. But in order to get more flexibility, you would always use JPanels. For example, to employ different layouts in different parts of the GUI, to group certain components together, etc.
A JFrame is backed by a JRootPane, a part of which is a contentPane.
(image from Oracle Javadoc)
When you add components to a JFrame, you are really adding them to the content pane, e.g.: frame.getContentPane().add(Component).
A JFrame is a common starting scene of a Swing GUI application, while a JPanel is intended to be put in another scene (container). Since both content pane and a JPanel inherit from the same class (Container) you may use them in a similar manner, as far as adding components to them goes.
Do I need JPanel always?
No. Well, unless you need a Swing GUI. Then yes.
Another answer replied words to the effect. "No, you can add components direct to a frame" What they missed was that components added to a JFrame are added to the content pane (automatically). The content pane is a JPanel.
Having said that:
I (and many others) would recommend designing an app based around a main content panel, then adding that panel to a top-level container as needed. The top level container might be a JFrame, JWindow, JDialog, JOptionPane ..
What prompted the question? A JPanel is a very 'light weight' container (in more ways than one). A GUI can contain 1000s and not be burdened by doing so. Of course, that's a rare requirement, but just saying .. use panels as needed and don't worry about it.

Adding Multiple Jcomponents to a Jframe and calling repaint on all components

I have questions regarding JFrames and Jcomponents.
Firstly, Can we add multiple Jcomponents
to a JFrame?
Secondly, if we do add multiple jcomponents to a JFrame then how do we repaint all of them when we want to do so ?
You're always going to have a tree structure in your UI, because JFrame is the root of it. So yes, you can have multiple components but they will be siblings in a parent. Use that parent (eith the JFrame or its content pane) as the target for your repainting calls, and it should propagate through the entire UI.

JFrame resize to visible components

I have an application where I want that the user is able to choose between normal and advanced settings. Now if the user checks a JCheckBox and the advanced settings should disapper the problem starts.
My idea was to set all unnessecary swing components (JScrollPane, JLabel...) invisible and then find a method of JFrame which fits the window to the VISIBLE components.
My question is if there is such a method?
... and then find a method of JFrame which fits the window to the VISIBLE components. My question is if there is such a method?
Yes, there is such a method, and it is called pack().
This will cascade through the layout managers of all the containers held by the top-level window, asking them to re-lay out their visible components, resizing components to their preferred sizes as based on the components and the layout manager requirements, and eventually resizes the top-level window to fit the containers and their components.
1. You can use setVisible(boolean b), to make the component visible and invisible.
2. You can check that if the component is visible or not using isVisible()
3. You can then use the pack() method, pack() method gives sets the frame size as per need
I think you can wrap the advanced content in a panel (if possible) and remove that panel from frame using this.remove(component) then use this.pack()
and you can do the opposite on showing them, this.add(...) then this.pack() again

Multiple frames in a Java application

Just need some quick guidance -
I have a main frame, and I want some smaller frames inside of it that can be dragged outside of the main frame (potentially onto multiple monitors). When dragged back in, they should not be hidden behind the main frame when the main frame is clicked on.
I'm unclear about what I should be using... JFrames? Frames? Windows???
I used two JFrames, but the lesser JFrame gets pushed behind the main JFrame when I click on it. Adding the lesser JFrame to the main JFrame's content pane gave horrible, nightmarish bugs. =)
Use JDialogs as the child windows and make sure you specify the JFrame as the owner of the dialog.

Categories