most suitable Panel layouts - java

Which LayOut Manager is most suitable for my GUI?
I need to make the above GUI but I didn't know which layout I basically used the flow layout(easy) just to set up all my components first.

Related

Java revalidate(); moves Objects that have been re-positioned, back to the original location

I'm making an application in which JLabels are created and can be re-positioned using
.setLocation(x,y)
However, whenever I invoke
.setVisible(true)
the JLabels move back to their original position. I think it has something to do with
.setVisible()'s --- revalidate() function
But I am unsure of how to override or avoid revalidate() effects.
Thanks,
You don't override or change revalidate() -- rather you use the appropriate layout manager since all revalidate does is to tell the layout managers to layout the components held by this and all nested containers. One "solution" is to use a null layout, but I strongly advise against that as this will lead to hard to debug and maintain GUI's, ones that might look good on one platform and terrible on all others. Consider nesting JPanels, each using its own layout manager, and using Borders, such as an EmptyBorder, to achieve your desired GUI layout.
If your program (not the user) is setting the location of the components then don't do this. Instead use the appropriate layout manager, or combination of nested panels with different layout managers to achieve your layout.
Read the Swing tutorial on Layout Managers for more information and working examples.
JLabels are created and can be re-positioned using setLocation(x,y)
However, if you are creating an application that allows the users to drag components around the screen then you need to use a null layout so you can control the location (and the size) of each component.
However, I would recommend you take at look at Drag Layout. This is a layout manager that implements most of the layout manager functionality, but will allow you to control the location of the component.
However, whenever I invoke setVisible(true)
Also, Swing components are visible by default so there is no need to invoke that method, except on top level containers (JFrame, JDialog etc).

JPanel and Java layout managers

Having read over the internet about JPanel for a while I feel I still need clarification after reading it. My questions:
What is JPanel mostly used for?
Which layout manager is most flexible to use?
How do you position components at certain areas using layout managers?
I find it really difficult to position components at certain areas within the container using a layout manager.
What layout manager do you recommend I use or do you have to use a mixture of different ones?
My first question is what is JPanel mostly used for.
You already know that. You add components to the panel.
My second question is which layout manager is most flexible to use
The more flexible that layout manager, the more complicated the layout manager is to use. So the trick is design your GUI logically and then use multiple panels with different layout manager to achieve your desire effect.
Each layout manager has its own strengths and weaknesses.
I find it really difficult to position components at certain areas within the container using a layout manager.
That is old thinking. You should not be trying to position components specifically. You position components generally and let the layout manager adjust as the size of the frame is changed by the user.
Maybe you want components centered, or in a grid. There are different layout managers to achieve your task. We can't give specific advice. If you have a specific problem then as a specific question and post your code that shows what you have tried.
But first, read the Swing tutorial on Layout Manager and download the example code play with the code to understand how each works. You learn by trying.
What layout manager do you recommend I use or do you have to use a mixture of different ones.
It depends on the GUI. I'd typically use a combination of layout managers for anything but the most trivial user interfaces. On the other hand, some like to try and use a more complex layout for those same GUIs, using layouts like:
GridBagLayout - older, but quite versatile.
GroupLayout - newer, more powerful, and provides better ability to align elements that are not next to each other, but considered so difficult to hand write that most use a GUI designer that will produce code that will not be portable to the next GUI designer or IDE.
A third party layout manager like MigLayout or FormLayout
JPanel can be used:
As a container for other components
Base for a custom widget
Drawing area
MigLayout is hands down the most flexible layout manager.
There are many layout managers in Swing, but in my opinion
only three are up to the job:
MigLayout
GroupLayout
FormLayout
Since MigLayout is an (greatly) improved FormLayout, I usually
recommend the first two managers.
Using a mixture of various managers is a poor practice. It is a way
of overcoming the simplicity of basic layout managers. One should choose a good layout manager and not to tangle with multiple layout managers. Most layouts can be easily done with MigLayout and GroupLayout.

Why the JInterFrame not resizing?

I am creating one java Application which includes the JDesktopPane and JInternalFrame.
And I am using GridLayOut to set JInternalFrame
The Issue is I can't resize the JInternalFrame as per the GridLayout size
Below is One Screen Shot of Application
I want the JInnernalFrame as the grid size.
Please do help me.
Because JInternalFrames are suppose to be added to and managed by a JDesktopPane
Take a look at How to Use Internal Frames for more details
JDesktopPane isn't suppose to have a layout manager, the GridLayout is taking control and making decision about how the JInternalFrame should be managed.
Simple answer is, don't use a layout manager with a JDesktopPane. Instead use JInternalFrame#pack to size the frames, setLocation to place it and don't forget to make it visible
I want the JInnernalFrame as the grid size
Either don't use JInternalFrames this way, or provide functionality that can mimic this when you want it. Layout managers tend to be to rigid for providing both these functionalities

Java Swing Application Fullscreen mode

I'm working on a Swing Application. The application needs to be in fullscreen all the time including its components. I made the application fullscreen using frmSwingapplication.setExtendedState(Frame.MAXIMIZED_BOTH);
The JFrame contains 2 JPanels. One has JTree and upon the selection of node of the JTree, a new panel is painted in another JPanel.
The problem is, when the application is minimized, it looks like
However when I maximize it, it looks like That is, only covering the portion it did while minimized. How can I resolve this issue? I tried setting size and bounds of container I got from getContentPane() but it was all useless. I even tried it with frame.pack(); and frame.setVisible(true);
You have to use proper layout managers:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Certain layout managers will use all the available space and size/lay out components inside them to fill the whole area. Examples for such layout managers are BorderLayout, GridLayout.

Adding more than one JPanel in a single JFrame

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

Categories