Hi :) I'm making my first GUI with Java swing, and I have a problem: when I change window dimension, "internal components" don't change their size.
I use absolute layout, it may be an important thing to know.
Absolute layout means that you are using absolutely defined constraints about position size e.t.c. To create resizable GUI. you need to use more "flexible" layouts.
One simple option to start is BorderLayout. Hovewer you have to leverage use of panels in this case.
Documentation
Another possibility may be the Grid bag layout but that often involves more work to do.
You can solve this problem by using the MigLayout in combination with the WindowsBuilder. This is a WYSIWIG editor for SWT and Swing Layouts. But BorderLayout and GridBag Layout can srink and grow as well.
Related
I would like to ask if SpringLayout can do anything like absolute position because I think absolute position have problem when I maximize the frame and what do I have to use if I need to set JMenu and JToolBar and JTextField and JTable all in one line in order?
I tried to use Borderlayout but it give me very big JTextField. I tried Gridlayout it give also big JTextField I need it big but not as big as it shows up.
can SpringLayout do all the job
No. It is neither designed, nor intended to do 'all the job'.
Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.
For absolute positioning, don't use a layout manager at all.
However, that's not really recommended. Usually you should use a layout that would scale.
See also this thread for more discussion.
i found note writing by java says
Note: This lesson covers writing layout code by hand, which can be
challenging. If you are not interested in learning all the details of
layout management, you might prefer to use the GroupLayout layout
manager combined with a builder tool to lay out your GUI. One such
builder tool is the NetBeans IDE. Otherwise, if you want to code by
hand and do not want to use GroupLayout, then GridBagLayout is
recommended as the next most flexible and powerful layout manager
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
Okay, I spoiled myself by playing around with GTK layouts, and now I've started thinking about windows layouts in GTK's terms and can't find an appropriate LayoutManager to use.
I'm specifically looking for a LayoutManager written in Java, for use in a Swing-based application that acts almost exactly like GTK's Horizontal and Vertical boxes.
In particular, I want the child widgets fixed to the off-axis size of the container, and also the option to have certain components take up the extra space (the "expand" option in GTK) while the other widgets only take up their minimum on-axis size.
Is there a layout manager that will do this?
In Java, the most configurable layout manager is the GridBagLayout manager. It allows you to specify the position in a grid using gridx and gridy and how extra space is allocated along that axis using weightx and weighty.
GridBagLayout is quite fiddly, so I would suggest reading a tutorial before using, but I have never found a layout I couldnt build with GridBagLayout.
You should use nested layouts. If you understand the grid box flow border layouts in java which is easy then you can nest them to make anything you want. i would give you some java code to do it if i understood how you want to layout your components. By the way you may want to try mig layout http://www.miglayout.com/ . I don't use it my self because i all ready know how to use swing layouts but its a nice library.
You can use BoxLayout - Oracle tutorial - and javax.swing.box for a component with BoxLayout.
It can manage Vertical and Horizontal layouts.
While I've not used GTK myself the way you describe it sounds like the requirements to which I implemented MSBLayout:
http://msblayout.sourceforge.net/javadoc/de/winterdrache/layout/MSBLayout.html
It combines the properties of BoxLayout and GridBagLayout that other posters have already mentioned, but it is much easier to use.
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 a JFrame in which i have to insert JLabels, textfields and JButtons. I am able to these but how can i adjust them to the required position, i want to add one label and textfield in one row and then nxt label and textfield in the next row but they are coming in the same horizontal line. i have used flowLayout with the JFrame. please tell me how to adjust them accordingly. thanks
The key to distributing components in a Container in Swing is the Layout Manager. There are various types out there. To do what you are looking for, you might want to consider the GridLayout. It is pretty easy to set up. You first need to create the layout. The following will create a two columned layout with as many rows as you provide:
GridLayout gl = new GridLayout(0,2);
Then you apply it to your panel:
JPanel panel = new JPanel(gl);
Then you add your items:
panel.add(textfield1);
panel.add(button1);
panel.add(textfield2);
panel.add(button2);
The GridLayout will handle moving from row to row after you fill in the columns with components.
had a look at gridbaglayout? Should serve your purpose.
A GridLayout may be what you want, or a combination of a GridLayout and a FlowLayout. Look at the LayoutManager tutorial to get a better idea of when and how to use and combine the various layout managers.
You need to study the various types of layouts swing provides.
Also you can have a look at FormLayout,provide by JGoodies. I prefer to use this than swing layouts as i find it easy to code and less lines of code
You are using the default Swing Layout Manager. If you want different behaviour (which is very reasonable) then you need to use another LayoutManager. Several exists both from Sun and "out there".
In order for you to be able to choose, you need to know how they work. I can strongly recommend using the Java Tutorial for this:
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
Let us know if you need more than provided by e.g. nested BorderLayout's or TableLayout.
If you want to be able to position the UI elements in your application (nearly) absolutely, consider using a decent GUI builder like Matisse in NetBeans or Swing UI Designer in IntelliJ IDEA.