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).
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.
Recently, I started creating a program for the company I work for. Just as background info, I'm still a student and a beginner programmer, so my solution is probably not recommended and I didn't know how to do it otherwise, but it works and I'm not going to be judged for it because it's a student job totally unrelated to programming.
The thing about the program is, it's going to be run on multiple different computer with different screen sizes and resolutions (800x600 and up). To make sure it takes as much of the screen as possible without losing any part of the program, I set the layout to null and hard-coded everything using relative values.
The program is kiosk-style and I first get the screen size values and go from there (for example, off the top of my head, the left-side menu takes an eighth of the screen, the top bar 2%, etc.). I also use font metrics to make sure the components are sized correctly and that everything gets displayed nicely.
My question is: why is it so frowned upon to make the layout null instead of using the layout managers? (I was told on some forums that this is a horrible way of doing things) I know how the layout manager works and know how to use the different layouts, but for the requirements of this program (multiple different resolutions, custom button shapes and placements, text changing on the components when you change language, etc.), I couldn't see myself using the layout managers to do it all.
How do you more experienced programmers use the layout managers in a situation like this? And what do you do when you want a button to be somewhere specific and other components somewhere else specific that don't really match any of the predefined layouts?
If you layer the layout managers correctly the screen will re-flow to different sizes for you, the idea is to use a single set of layout managers on ALL screen sizes.
If you use null you will have to do each screen size yourself. Not only that but if the app can be windowed you have to support every possible size they might scroll to.
That's kind of difficult to do, but the layout mangers are designed to do just that.
There are some common tricks. BorderLayout is a great layout to start with. Sometimes you might use it at multiple levels--often with just 2 or 3 components in it. That's because it's really good at giving all but one area the minimum required area and giving everything else to the CENTER.
FlowLayout can be useful but it's tricky if your components are different sizes.
I wouldn't try GridBagLayout unless you are planning to write code to feed your layout manager (an excellent solution at that!).
I also wouldn't use GUI builders, they don't know the overall way you want to reflow your layout.
In a nutshell: because all the work that you explain above is done (or at least: should be done) by the layout manager.
More often than not, when a null layout is used, it also implies that all positions and sizes are hardcoded to a single value, so no flexibility at all is given. This means that changes in window size, language, font size, display density or any other related parameter have no effect on the layout and you get the usual ugly effects: empty parts of the window; tiny, unresizable lists; buttons with their labels cut off; ...
It sounds like the work you do should really be done by the Layout Manager. Either find one that does that (my personal suggestion would be MiGLayout, which does a lot and is easy to use) or write your own.
You are practically using a layout - your own, with all your sophisticated calculations of positions.
You can move these logic to a custom layout manager class to pacify the critics.
hmmm trick should be by mixing LayoutMangers and by usage of numbers of nested JPanels that each could have diferrent Layout or not, really depends of number of JComponents, that allows you to create GUI that looks like as layed by using AbsoluteLayout but with same look/output to the GUI for every screen resolutions and ratio (4:3, 16:9, 16:10)
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.
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)