I have a swing project and noticed that the elements inside the JFrame is statically located inside the JFrame and I do not have the option of resizing the elements as I want to.
My gui app inside the designer window looks like the following:
I want to resize the button (E.g.) by dragging the corners of the button, but I am not allowed to?
As you can see on the following picture, the dragging is not allowed per pixel, but only per section in the JFrame:
How can I disable the static placement of the elements/Enable the self-dragging of elements inside the designer window?
Most likely you will need to disable the LayoutManager. On Netbeans, Setting this to null would provide you full control over the location and dimension of the child elements, so I am assuming that something similar should work here (although you seem to be using Eclipse, if that is the case, please state what plugin you are using).
It is to be noticed however that usually you want to have a layout manager taking care of how your components are rendered. I would recommend you take a look here for some more information on layout managers prior to removing them completely.
Setting size and position of components in a GUI should be left to the JRE at run-time, using:
The preferred size of the component (with current content, in the current PLAF).
The borders/padding/insets set to the component.
The layout used.
The component spacing defined in the layout constructors.
The layout constraints used when adding components to the layout (especially important to e.g. BorderLayout and the much maligned GridBagLayout, as well as many 3rd party layouts).
Generally, complex effects are created by using a nested layout.
Related
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.
I have created a JFrame - now I want to add the 4 JPanel in that frame at a particular location. How can set the location of panels in the frame?
Use (possibly nested1) layouts for the logic. See Laying Out Components Within a Container for details. They can:
Include default spacing in the constructor (often)
Calculate how big the GUI needs to be in order to display the components (in whatever PLAF, on whatever system the app. is deployed).
Extra spacing can be organized by adding an EmtpyBorder to child components.
See the nested layout example
Placing components in a container is quite a complicated subject in Swing. Instead of defining the exact places for your components, you would normally use a layout manager that arranges them in a certain way.
Here is the tutorial you should read to get a (visual) clue about the different layout managers: A Visual Guide to Layout Managers
However, the standard layout managers of Swing can be cumbersome for more complex layouts. Either, you could use nested layouts to get the desired result, or you could use a very powerful third-party library: JGoodies Forms. The downside is of course that you have to learn yet another library. Therefore, I would only recommend it for a bigger project.
For me it is good way to set GridbagLayout for the container of the frame. There are several visual swing GUI editors available to do this easily. You can use NetBeans GUI editor or GWT Designer (https://developers.google.com/web-toolkit/tools/gwtdesigner/) for complex GUI designing tasks
If its 4 locations, you can use BorderLayout,by default its the CENTRE, but it also have EAST, WEST , NORTH, SOUTH locations for the placement of the components. You can also use setLocation to put the panels in the appropriate locations, if a layout isn't used.
Its even better to use GroupLayout developed my NetBeans team in 2005, use Windows Builder Pro, now provided by google for free.
set the layout of the Frame to be null via setLayout(null)
create 4 JPanel and set their location using setLocation method
add these panels using JFrame's add method
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 am creating an application which has a scrollable and scalable (zoomable) view. Inside this view I want to place other components (most of them customized JPanels and JInternalFrames).
Things such as fonts and borders, and sub-elements such as buttons don't need to be scalable. Only dimensions and position of container components should be.
What do you think, what is a good way to implement scalable components?
EDIT: I'm talking about resizing the entire layout including all
components. Please think of something such as a visual UML editor with zoom functionality.
My alternatives are:
Create a custom layout manager;
Create custom resizeable sub-components;
Create a custom container which would take care of resizing its sub-components;
Do something else?
Possible problems:
Boilerplate code;
Necessity to provide access to additional custom properties of components;
Not straightforward (inconsistent) representation of components in code.
Something else?
This is why layout managers exist: they tell contained components where and how large they should be.
Since you're talking about a UML editor, are you using contained Swing components to represent the various objects in the diagram (eg, a component for a class)? If yes, then you've got a lot of work ahead of you (although it's not necessarily a bad approach). I'd recommend creating a constraints object that identifies the object's location on a "unit space," then multiplying by the current size.
I've done that by creating a custom layout manager. Every component (or rather component class) on the frame has a marker whether or not it shall be resized when the container is resized (e.g. tables are resized, buttons are not). Those which are not resized are moved when the container is resized.
This is used to make resizeable forms without any manual setup, i.e. forms are defined by just specifying x/y/length/width for each component (no further alignment info).
If I look at this problem as visual UML editor then I had to think about single "canvas" component drawing each element as graphical object with base aspect ration and zooming in/out. I can't see reason for list of components aligned within parent container.
I started a similar solution that works fine without touching the original layout.
It's as easy as this:
// Install scalable layout and CTRL+/CTRL- keys for scaling operations
ScalableLayoutUtils.installScalableLayoutAndKeys(new DefaultScalableLayoutRegistry(), frame, 0.1);
It's still under work but It will be soon available as part of the next "utils4swing" version.