Anyone explain to me the difference between content pane and layout with by example. Give code example where exactly need of content pane (or) layout.
What is the difference between these, and in what circumstances should you use one rather than the others?
There is no comparison between content pane and layout; they are two distinct entities.
Content Pane :
The default content pane is a simple intermediate container that inherits from JComponent, and that uses a BorderLayout as its layout manager.
Layout
It is used to place components on the parent container. These are basically the set of constraints, which establish the position of a given component on the parent container.
They both are used as you make a Swing Application. As when you write
frame.add(childComponent);
the childComponent is actually added to the content pane. Three methods, add(...), remove(...) and setLayout(...), are overridden for content pane.
Related
In my GUI class I have an array of textfields, which I initialized in this class constructor. The code is as follows
JTextField[] arrayOne= new JTextField [10];
for(int a = 0;a < 10;a++)
{
arrayOne[a] = new JTextField(String.valueOf(0));
arrayOne[a].setBounds(189 + a * 50, 156, 39, 23);
arrayOne[a].setVisible(true);
}
The problem is, that when I run my GUI, it does not show these text fields. Why not?
You should add the JTextField in the layout.
You should use add method of the container/or the class if you extend JFrame inside the for.
Read this.
Adding Components to a Container
When you add components to a panel or content pane, the arguments you
specify to the add method depend on the layout manager that the panel
or content pane is using. In fact, some layout managers do not even
require you to add the component explicitly; for example, GroupLayout.
For example, BorderLayout requires that you specify the area to which
the component should be added (using one of the constants defined in
BorderLayout) using code like this:
pane.add(aComponent, BorderLayout.PAGE_START); The how-to section for
each layout manager has details on what, if any, arguments you need to
specify to the add method. Some layout managers, such as GridBagLayout
and SpringLayout, require elaborate setup procedures. Many layout
managers, however, simply place components based on the order they
were added to their container.
Swing containers other than JPanel and content panes generally provide
API that you should use instead of the add method. For example,
instead of adding a component directly to a scroll pane (or, actually,
to its viewport), you either specify the component in the JScrollPane
constructor or use setViewportView. Because of specialized API like
this, you do not need to know which layout manager (if any) many Swing
containers use. (For the curious: scroll panes happen to use a layout
manager named ScrollPaneLayout.)
For information about how to add components to a specific container,
see the how-to page for the container. You can find the component
how-to pages using How to Use Various Components.
There is one more case for non visibility.
f.setVisible(true);
f.setSize(1000,1000);//Where f is the JFrame reference
Add the above statements after adding the JTextFields to the required Frame.
Why JMenuBar is not place in the content pane?is there any reason or effects when make a java gui program especially when using jframe?
Thanks
As stated in Using Top-Level Containers article, the manu bar is managed by the Root Pane:
Each top-level container relies on a reclusive intermediate container
called the root pane. The root pane manages the content pane and the
menu bar, along with a couple of other containers. You generally don't
need to know about root panes to use Swing components. However, if you
ever need to intercept mouse clicks or paint over multiple components,
you should get acquainted with root panes.
A menu bar is tipically placed at the top of a window, so if it would be placed in the content pane then the Layout managers would have to deal with this particular component. By segregating the menu bar from the content pane then the responsibility of laying out the menu bar and the rest of components is well divided. In addition, the menu bar location is fixed and this component is not dragabble so this can be laid out in a different way than the rest of components.
About the JToolBar this component is draggable and you may have several of them, placing one above another one and rearranging their order as you wish. So this component is totally different of a menu bar and makes sense it be placed in the content pane just as a text field or another regular component.
Suggested readings:
Using Top-Level Containers
How to Use Root Panes
How to Use Menus
Not sure if what I need is possible.
I have a container (JPanel) that contains some internal elements.
I was wondering if it is possible to force internal elements to fit into the container's size.
I need them to be fully visible i.e., resize to fit inside the Panel's size and not cut some parts of the internal elements.
Scrolling is not an option.
Is this possible by using a Layout or something?
EDIT: Important clarification:
The thing is that I do not have access to the internal elements neither to their properties so I would say that a Layoutmanager capable of resizing child elements to fit to its size is needed. I tested BorderLayout and GridBagLayout but the result is always the same, the internal elements are cut out.
It's for exactly that reason that LayoutManagers exist. All the LayoutManagers work for simple containers directly, excluding GridBagLayout which is to able to handle most complete GUIs directly.
For most complete GUI's you have some choices as follows:
Look for a 3rd party layout such as MigLayout or here
Use GridBagLayout
Very easy way is use nested layout, where there is more than one JPanel and each has child JPanels with the same or different LayoutManager
Or custom layout, should be hard..., but same as using GridBagLayout
You could set the JPanel layout to border layout, then add the single child to the center. If there are multiple children, this approach becomes less useful since components added to the the NORTH, SOUTH, EAST, and WEST will remain statically sized while the centre resizes to fill the remainder.
In short, this isn't an ideal solution. All layouting in Swing is made all the more complex by the fact that different components behave in different ways, so you really need to provide further details of the child components you wish to add to your panel, and any behaviour that has been overridden on those components.
The best way is to try a couple of simple examples to see what mileage you get and whether subtle redesign of your child component nesting could help.
you can use a layout, like GridBagLayout, or BorderLayout depending on the situation. With proper weights it is possible.
this sounds to me like you should just peek an appropriate layout manager and use it. For example, look at BorderLayout - put your component in the CENTER and it will occupy all the area. Its up to each concrete layout manager to decide what will be the size of the components.
Mark
I was using a JInternalFrame inside JDesktopPane. I wanted the internal_frame to auto resize as desktop pane is resized, so I had to implement the AncestorResized event for the internal frame where I placed the following code:
this.setPreferredSize(this.getParent().getPreferredSize());
this.pack();
What is the difference between Java frame functions getContentPane() and getRootPane()?
Also what wil happen when we set a JButton as Default.
from documentation:
getContentPane() is generally implemented like this:
public Container getContentPane() {
return getRootPane().getContentPane();
}
It's well described in Swing tutorial (here).
While using top-level containers in AWT or Swing, the root pane is the base pane.
The hierarchy is as follows:
Glass Pane: Generally hidden, setting to visible will show up a glass cover over the root pane areas.
Layered Pane: Contains the Menubar and the Content Pane
Content Pane: Is the basic layout pane in which components are actually placed.
Calling the method getRootPane() will return the reference to the base pane, while calling the getContentPane() method will get you the reference to the Content Pane. It is visible by default.
By setting Jbutton default, What are you exactly trying to accomplish?
The Root Pane, as the name implies, is the root of the frame/window/dialog.
Its contains als other components of this top-level component.
the content pane is one of the four parts of the root pane and contains the components. the other parts of the root pane are glass pane, layered pane and a optional menu bar.
the tutorials at oracle explains this really good:
http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html
I found three ways to fill my JFrame frame = new JFrame("...")
createContentPanel returns a JPanel and createToolBar returns a ToolBar.
frame.add(this.createToolBar(), BorderLayout.PAGE_START); //this works and puts the ToolBar above and the ContentPanel under it<br>
frame.add(this.createContentPanel(), BorderLayout.CENTER);
frame.setContentPane(this.createContentPanel()); //this lets the JToolBar hover over the ContentPanel
frame.getContentPane().add(this.createToolBar());
frame.getContentPane().add(this.createContentPanel()); //this only puts the last one into the JFrame
frame.getContentPane().add(this.createToolBar());
And now I am wondering why should i use the getContentPane()/setContentPane() method if i could just use a simple frame.add(...) to fill my frame.
You are right that it doesn't matter which you use (JFrame#add(...) vs. JFrame#getContentPane().add(...)) since they both essentially call the same code, however there will be times in the future when you'll need access to the contentPane itself, such as if you want to change its border, set its background color or determine its dimensions, and so you'll likely use getContentPane() at some point, and thus getting to know it and be familiar with it would be helpful.
//this only puts the last one into the JFrame
You need to understand how layout managers work. The default content pane is a JPanel that uses a BorderLayout. When you add a component and don't specify a constraint, then it defaults to the CENTER. However you can only has a single component in the center so the layout manager only knows about the last one added. When the layout manager is invoked it sets the size() and location() of that component. The other component has a size of 0, so it is never painted.
In Java 1.6, you can just use the add method of JFrame:
http://download.oracle.com/javase/6/docs/api/javax/swing/JFrame.html
(It will be delegated to the contentPane.)
http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JFrame.html
Which says:
The JFrame class is slightly
incompatible with Frame. Like all
other JFC/Swing top-level containers,
a JFrame contains a JRootPane as its
only child. The content pane provided
by the root pane should, as a rule,
contain all the non-menu components
displayed by the JFrame. This is
different from the AWT Frame case. For
example, to add a child to an AWT
frame you'd write:
frame.add(child);
However using JFrame you need to add the child
to the JFrame's content pane instead:
frame.getContentPane().add(child);
The same is true for setting layout
managers, removing components, listing
children, and so on. All these methods
should normally be sent to the content
pane instead of the JFrame itself. The
content pane will always be non-null.
Attempting to set it to null will
cause the JFrame to throw an
exception. The default content pane
will have a BorderLayout manager set
on it.