Problems with my GridLayout - java

I have a GridLayout 2 rows by 5 columns, and I want to make the height of the first row to something like 50, and the second row to 200. I know GridLayout creates equally-sized cells, so this didn't work out. I also tried splitting the two rows into two GridLayouts setting their desired heights and adding them to a FlowLayout, but the columns didn't align the way I wanted it to. My code went something like this:
row1.setSize(WIDTH, 50); //GridLayout
row2.setSize(WIDTH, 200); //GridLayout
panel.add(row1);
panel.add(row2); //panel is a FlowLayout
The columns aligning are very important and I can't seem to get this right.

I'd look into GridBagLayout. Although, almost everyone I've talked seems to dislike it. I like it, though.

You can't do this with a GridLayout.
You should be able to use either a GridBagLayout or a SpringLayout. Check out the Swing tutorial on Layout Managers for some examples to get you started.
Also you don't add individual rows to the layout. You need to add all 10 components individually to the same panel using whatever layout manager you choose.

Each cell in a GridLayout is exactly the same size, so you'll have to use a different layout manager, e.g. GridBagLayout.

Related

How to setSize JButtons in a JPanel

I'm trying to fix this issue in my JPanel.
I want to set sizes of 0 and = JButtons in JPanel. Other button sizes are the same but 0 and = are bit long vertically and horizontally (like MS windows Calculator). How do I set it?
I think you can achieve that by using GridBagLayout layout manager.
A GridBagLayout places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. Not all rows necessarily have the same height. Similarly, not all columns necessarily have the same width.
See How to Use GridBagLayout.
If the layout used respects the preferred size of the buttons, a larger button can easily be had one of three ways:
Set a larger size of Font
Give the button a larger Icon
Call setMargin(Insets) with a large margin.
If you don't want a hassle, try using WindowsBuilder on Eclipse. You can edit it on a design interface, and the codes will be generated.

Which Panel to use in Swing

I would like to have a JTable component in one panel to take up as much space as the window allows. Underneath, however, I would like to add another, fixed size panel with a few buttons in it. WHich layout to use to allow the bottom panel be of fixed size, while allowing JTable panel to stretch according to size of the window?
It's hard to visualize what you want exactly, but here's an excerpt from Oracle that may help you:
Scenario: You need to display a component in as much space as it can get.
If it is the only component in its container, use GridLayout or BorderLayout. Otherwise, BorderLayout or GridBagLayout might be a good
match.
If you use BorderLayout, you will need to put the space-hungry
component in the center. With GridBagLayout, you will need to set the
constraints for the component so that fill=GridBagConstraints.BOTH.
Another possibility is to use BoxLayout, making the space-hungry
component specify very large preferred and maximum sizes.
Scenario: You need to display a few components in a compact row at their natural size.
Consider using a JPanel to group the components and using either the JPanel's default FlowLayout manager or the BoxLayout manager.
SpringLayout is also good for this.
Scenario: You need to display a few components of the same size in rows and columns.
GridLayout is perfect for this.
Scenario: You need to display a few components in a row or column, possibly with varying amounts of space between them, custom alignment, or custom component sizes.
BoxLayout is perfect for this.
Scenario: You need to display aligned columns, as in a form-like interface where a column of labels is used to describe text fields in an adjacent column.
SpringLayout is a natural choice for this. The SpringUtilities class used by several Tutorial examples defines a makeCompactGrid
method that lets you easily align multiple rows and columns of
components.
Scenario: You have a complex layout with many components.
Consider either using a very flexible layout manager such as
GridBagLayout or SpringLayout, or grouping the components into one or
more JPanels to simplify layout. If you take the latter approach, each
JPanel might use a different layout manager.
Source: Oracle: Using Layout Managers
The simplest way to achieve it is to use BorderLayout. Put your table in the center. Then create yet another panel with FlowLayout and put it to the south of your main panel.

add a scrollable jpanel to a gridlayout

I'm building a grid filled with labels. One of them contains html-text and should resize to maximum format and be scrollable. I found how to add a JScrollPane but it stays one line height, I just can't find how to resize it even when I give it a size of 400x400 ...
Removing getViewport() gives the same result.
JPanel grid = new JPanel(new GridLayout(1,2));
// first cell of the grid
grid.add(new JLabel("title"));
// second cell of the grid, this should be the scrollable one
JScrollPane scroll = new JScrollPane();
scroll.getViewport().setSize(400, 400);
scroll.getViewport().add(new JLabel("<html>long<br>html<br>text</html>"));
grid.add(scrollVersion, BorderLayout.CENTER);
Any ideas ?
Thanks a lot ...
GridLayout does not respect preferred size of the components which it lays out. It aims to make all grid cells the same size. An alternative is to use GridBagLayout, however I personally would recommend ZoneLayout which (in my opinion) is simpler, just as powerful, and much more intuitive. With the cheatsheet you can't go wrong.
As a side note, BorderLayout.CENTER is a constraint used for BorderLayout and is not compatible with GridLayout. When components are added to the owner of a GridLayout, you need not provide constraints. Components are added left to right starting at the top left corner cell using GridLayout.
Replace your GridLayout with a GridBagLayout. With the correct set of constraints, it should work like a charm. And obviously, take a look at some examples, as GridBagLayout seems quite complex, but is rather simple with some examples.
All cells of the GridLayout are designed to have the same size, so if you want one to be bigger than teh othes you must use another LayoutManager, like the GridBagLayout that Riduel suggest.
Also if your JLabel is going to have more than one line i suggest you to replace it by an uneditable JTextPane o JTextArea

Swing layout - Using a grid while keeping component dimensions

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.

Adjusting positions of swing components

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.

Categories