I have a screen in my application. The layout of the screen is shown in the attached image file.
I have to add upto 5 labels in Panel1111. But, When I try to add labels in Panel1111, the Panel11 resizes and Panel12 shifts downwards to give space to Panel11.
I want to overlap content of Panel1111 on Panel12.
How can I achieve it?
Layout details:
Panel1 : BorderLayout
Panel11: OverLayLayout
Panel111: GridBagLayout
Using JLayeredPane. Go to Oracle Java website, and go through the tutorial: How to Use Layered Panes
Java's Layout Managers by default try to show all information that is inside them.
If you say you want two panels to overlap, this essentially means that the lower one cannot be seen fully, and also not interacted with in the hidden/overlapped part. Then, this part of the panel doesn't make sense any more. So you should probably rethink your GUI.
If you want it to overlap only at certain times, and the user can define when it should overlap and when not, then you'll need to handle that manually by using no Layout Manager at all, but position the elements yourself. Oracle provides some hints how to do that: http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html.
In the end, you might end up writing your own, custom Layout Manager to handle the resizing of the panels.
Note: only the the layout of Panel 1 must be manually managed. The other panels can likely be handled by a LayoutManager again.
Related
I am trying to create a GUI for a program with an undefined number of rows but stay with two columns. Currently the only way I can get it to look how I want is using FlowLayout but the window of course needs to be smaller than desired. It needs to happen within one panel as well because it is being added to a tabbed pane.
What would be the best layout manager to solve the problem that is in the JDK? Or would just kind of brute forcing it with AbsoluteLayout be the best approach (as the user shouldn't really be resizing the window)?
I have attached the desired appearance.
Thanks for any help in advance.
Several layouts can do what you want. I'd suggest GridLayout (easy to use, but columns will be equal width) or GridBagLayout (harder to use, but you have lots of control). You could also use a BorderLayout and put all the fields in a sub-panel on the WEST and all the drop-downs in a sub-panel on the EAST. The difficulty with that is ensuring that the rows have the same height, since they won't be constrained by the layout itself.
The best thing to do would be to go through the Java tutorial on layouts and get up to speed on what the various layout managers can do.
Also, since you're using Swing, you could just use a JTable (as Gilbert Le Blanc suggests in his comment).
I am in the process of completing a course work but I need some help understanding this:
"Note: Your application should keep the relative positioning of buttons and checkboxes
when the frame of the application is resized based on the layout managers and the tech-
niques covered in the module. I.e. you should not choose absolute coordinates when
placing JComponents on the screen but relative positioning, i.e. component X is to the
right of component Y and to the left of component Z and these relative locations should
be maintained if the user resizes the frame of the application."
Does this mean im not to use BoxLayout.X/Y_AXIS? Absolute coordinates?
To my understanding I have to use several panels? If so, my code contains a login and when I login how will I change the colour of the frame?
There's basically two ways to layout the components in a JFC/Swing application:assign each container a layout manager that will take care of the layout; see the link provided by #Shakedownassign each JComponent an absolute position for it's top left corner, counting pixels from the top left starting at (0,0) of the top-level-container (see here for what that is) the components are in; see this tutorial on absolute positioning.
As you can imagine, the second way has some problems: it's not at all dynamic and is easy to get wrong, to name the most prominent ones. Using a layout manager in most cases is not only the easier, but also the smarter way to arrange your GUI.
What the layout manager in effect does is assign each JComponent an absolute position derived from the layout the component is layed out by at runtime, dynamically - usually using the PreferredSize of the JComponents; for example, a FlowLayout will assign each JComponent an absolute position that will put it exactly fitting to the right of the JComponent before it, wrapping lines when needed. When a container is resized, all components within it will have their positions recomputed. See here for details.
Concerning your question about BoxLayout, that is a layout manager; BoxLayout.X_AXIS and BoxLayout.Y_AXIS are used to determine in which direction the JComponents which the BoxLayout manages are to be arranged; they do not refer to absolute positioning. See the Javadocs on BoxLayout and the Java tutorial on BoxLayout for details.
All in all, your task is to use a layout manager; the different layout managers that are available in the standard java libraries are described in sufficient detail in the tutorials (see links, browse the sites). Have fun!
This sounds like it's saying that you need to use layout managers instead of absolute positioning. Yes, you will end up using several components (likely JComponent or JPanel), each with their own layout manager. You can build up your UI by starting with standalone components and combining them together in another component with a layout manager.
Start here: Laying Out Components
If you've got specific questions such as changing the color of your frame, it'd be best to ask that as a separate question (assuming a similar question doesn't already exist).
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();
I'd like to make a login bar for an application and I can't figure out how to organize a series of JLabels and JTextFields such that they are organized in a horizontal grid without these same components being resized to fit each cell. I also want to make sure that the group of components isn't resized below a certain width. How can this be achieved?
Edit: Thanks for the answers everyone. I'll have a look at MigLayout and SpringLayout later. Due to time constraints I'm going to have to make do with Visual Editor and use a null layout. The component placement and dimensions have to be adjusted by hand but at least they stay put. Here's a picture showing what I wanted to do.
bar http://img145.imageshack.us/img145/7356/bargw.png
Use MigLayout as your layout manager, it's extremely flexible, and supports what you're asking quite easily. You can set size constraints. If you need any further help, post some example code using Swing and MigLayout which shows what you're trying to do, and then I'll advise you on how to do what you want to achieve.
You probably want some additional cells which 'grow' to fill the remaining space. This can be achieved with column constraints, by inserting 'push' between the columns (specified by [..]) to expand the gap. You don't need any placeholder components in this case. (i.e., [pref!]10px[40px::]push[pref!]10px[40px::])
You have to use different layout. FlowLayout or BoxLayout will work in your case, but I would suggest MigLayout simply because it will cover all your needs and replace all others .
Check out the section from the Swing tutorial on Using Layout Managers.
The SpringLayout has an example that does exaclty this.
The GridBagLayout is more difficult to use but also support row/column type layout.
Finally, you can still use a GridLayout. Just add the text fields to a JPanel first, then the panel will grow but the text field won't.
I have to build a rather large form with many controls. The controls are divided in basic controls/settings and extended controls/settings. The user can decide if he wants to see only the basic or both basic and extended controls.
I've dropped all extended controls onto their own JPanel so that I can easily switch between the two views by showing or hiding this panel.
Currently I'm using GroupLayout and what happens is that the controls on different panels are not aligned:
Label aaa: Text field
Label a: Text field
Label aaaaaa: Text field
----------------------------
Label b: Text field
Label bbb: Text field
Label bb: Text field
Unfortunatly I found now way to "synchronize" the layouts of the two panels (except using AbsoluteLayout and fixed control coordinates)
Is there any way to achive this?
Is my whole design flawed?
EDIT: If it is possible I would like to keep the GroupLayout manager.
As far as I know, no Swing LayoutManager (from JRE or open source) can span several panels.
I am currently working on such a feature (which I called "layouts synchronization") for my DesignGridLayout project, but it is not something easy to implements (I have started about 2 weeks ago and I still don't see exactly if and when I will get to something interesting, but I still have high hope for it;-))
One option you could check would be to add all components to the same panel (with just one GroupLayout then) and hide/show them based on user's selection. Hopefully, GroupLayout will adapt the size to the situation (after calling pack()).
If GroupLayout behaves well, then it would just be a matter of calling pack() each time after user changes his selection to show/hide extended fields.
Else you would have to manually set the size of your panel every time the user changes his selection.
Probably the easiest (good) way to do it is to add all the components to the main panel. Set the subpanels to non-opaque, and add the also to the main panel. The main panel the needs optimised drawing to be switched off.
Another technique is to add a spacer component. To the bottom panel add a component in the same column as the labels which dynamically takes the width component of its various size methods from the top labels. Do the same in reverse to the top panel.
I think there is no way to do it with the standard layout managers. You'll probably have to write your own layout manager, but it shouldn't be too hard if you subclass GroupLayout.
You could use GridLayout instead of GroupLayout which will give you uniform spacing between the columns
If you want to keep them in separate panels with separate layouts:
Iterate over all of the labels that you add, and find the maximum preferred width of each.
Iterate a second time, and set the preferred size to that each label's preferred height, but the maximum width.
This is the explanation of th GridLayout. This will set every component to the size, you expect it. With the GridData object you can specify how the components are ordere.
Examples
(source: sun.com)