Specify the number of grids in a GridBagLayout - java

GridLayout creates a grid matrix whose dimension (no of rows and columns) is customizable using two int parameters that is takes as part of its constructor.
Why don't we have such a similar option in GridBagLayout?
In a typical scenario using a Gridbaglayout, If i wanted a component to be placed at say extreme right or middle or at any other arbitrary location wouldn't it help if I know the size of Grid Matrix?.

Each 'row' of a GridBagLayout can have different numbers of columns, so it hardly makes sense to specify it in the constructor.

Because you specify the grid coordinates of a component through GridBagContraints when adding the component. The GridBagLayout’s size is then determined automatically by the largest coordinates (and probably the grid width and grid height of the component at these coordinates).

Related

MigLayout Column Based Alignment

I'm currently using MigLayout to try to accomplish a column based approach to swing. Essentially, I have different size components to layout on the screen with varying heights. Below is an image of what I have so far using MigLayout.
Example Mock Layout
As you can see, I've sorted the objects by their height. Each object is a JPanel. I'm making the assumption in building this model that the width of each component is statically defined. I would like the objects to not be defined as a grid, but as a set of columns where each component will be aligned with the component above it which will remove the padding between the components. I have been unsuccessful in finding a layout manager to accomplish this task. I believe MigLayout has the most customization to it which is how I got this far.
Any suggestions would be very much appreciated!

Java Layout for specific task

I need suggestion for layout type for following task. I have a panel on which user will be able to add or remove some components (label or another panel), which are all same size. There will be specific number of components at same column (like 4 components per column) but the number of components in rows will depend on user. The distance between components will be fixed, right, left up top. I will link you the image of what i need... Thanks.
link
I think you want a GridLayout, however I suggest using a third-party layout manager like MigLayout. For the case where you have empty cells, you can nest JComponents within each other with different layout managers ( see this SO question). MigLayout would be easier because it can simulate a GridLayout while respecting the preferred size you set on your JComponents, which allows you to have empty cells without the components stretching.

max size of JLabels being overridden by text width?

I have a JTable in which one column represents the size occupied by a folder on disk. Each row represents one folder, and this column has a list of rectangles; the width of each rectangle represents a percentage of the size occupied by one type of file. Here's a picture:
I do this by creating a JLabel for each file type in a row, then, in the getTableCellRenderer() method, I have an array of JLabels, one per file type. That method sets the preferred and maximum sizes of the JLabel to be the column height and the percentage of the column width for each label.
My problem comes when the picture is narrowed; the following was made by narrowing the frame of the program illustrated above:
As you can see, the percentage representation is gone, and the width of the JLabels now appear based on the width of the text, not on the preferred/max sizes set. If I narrow the window further, the percentage calculations again appear to take precedence; it is only in a narrow width range that I see this behavior.
Is there another way to do this? Do I have to draw rectangles and use drawText() instead of using the FlowLayout and text elision that already exists?
Here's the code that sets the characteristics of the JLabels:
sizeBlockDimensions[i].setSize(newWidth, rowHeight);
sizeBlockLabels[i].setPreferredSize(sizeBlockDimensions[i]);
sizeBlockLabels[i].setMaximumSize(sizeBlockDimensions[i]);
sizeBlockLabels[i].setBackground(color);
sizeBlockLabels[i].setText(labelText);
sizeBlockLabels[i].setVisible(true);
i++;
instead of using the FlowLayout
You should be able to use the Relative Layout. It was specifically designed for relative sizes and should adjust automatically as the space available changes.

GridBagLayout extends column

I have a JPanel with GridBagLayout set. It has 2 columns and 1 row (2 cells). Every cell contains one JPanel which contains one JLabel (type of component is insignificant).
The JLabel in the left cell has width attribute set to 100px. The JLabel in the right cell has width attribute set to 50px. In such case the left cell extends a little and it's wider than the right cell.
I thought that GridBagLayout expands cell only when contained components take too much space.
Is it a GBL bug?
Any idea how I can solve this problem?
Any idea how I can solve this problem?
Java Swing does not work the same way as CSS/HTML. You create the components in Swing and let the GUI worry about the sizing.
Without knowing what you're trying to accomplish, all I can do is say define the Swing components and let the GridBagLayout manage the layout.
If you want the grid areas to be the same size, you would use the GridLayout.
I am afraid this is a difficult question to answer because GridBagLayout doesn't arrange it's columns and rows this way. If you have two columns and two components the columns will size themselves as the sizes of the largest components put into those columns. So your left column is bigger, because your left component is bigger.
You can adjust how much of any spare space a cell takes by adjusting your component's corresponding GridBagConstraints attributes. The weight attributes (weighty & weightx) control how much of any spare space the columns (weightx) and rows (weighty) take up. If for example your JPanels were using GridBagConstraints objects called rightGbc and leftGbc you could use the following code to alter their relative sizes.
leftGbc.weightx = 0.5;
rightGbc.weightx = 1;
This means that the right column will take up twice as much spare space as the left column and therefore (hopefully) make up for the difference in the size of your components.
Using the weight attributes can feel like a very abstract process, and it can take a while to get used to them, but once you start using them for a while you will get it. Depending on which components you are using, there are other quirks which can effect how much affect the weight attributes have on the columns/rows.

List of contacts swing

I need to implement ui for list of contacts like in skype. An contact represented by custom class(JContact) which derived from JPanel. I tried to use different layouts but not received expected result. Main frame has next structure.
JFrame -> JPanel(contactsPanel)-> JScrollPane(scrollContacts)->JPanel(contactPanel)
scrollContacts.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollContacts.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
If use FlowLayout for contactPanel (see FlowLayout in image)
Strange behavior i think, because expected what each contacts will one under one because indicate HORIZONTAL_SCROLLBAR_NEVER for scrollContacts.
If use Grid or Box layout. Layout automatically re-size my panels, it's look very ugly. see Grid&Box layouts image.
Expected result see "expected" image
----SEE IMAGE----
I'm not native speaker, so please sorry for my bad English.Thank you for attention!
Quoting the Swing tutorial:
The FlowLayout class puts components in a row, sized at their preferred size. If the horizontal space in the container is too small to put all the components in one row, the FlowLayout class uses multiple rows
So the result you get is expected.
A GridLayout object places components in a grid of cells. Each component takes all the available space within its cell, and each cell is exactly the same size. If the GridLayoutDemo window is resized, the GridLayout object changes the cell size so that the cells are as large as possible, given the space available to the container.
So the result you get is also expected.
When a BoxLayout lays out components from top to bottom, it tries to size each component at the component's preferred height. If the vertical space of the layout does not match the sum of the preferred heights, then BoxLayout tries to resize the components to fill the space.
So the result you get is also expected.
But, a box layout can contain glue components to avoid that.
I would thus use a vertical box layout, and add a vertical glue as the last component. Read the tutorial.

Categories

Resources