Editing the height of a specific row in GridLayout Java - java

I want to edit the height of a single row in a JFrame with a GridLayout. Is there any way to do this or must the height of every row be constant?

Is there any way to do this..
Not with a single GridLayout.
..or must the height of every row be constant?
Yes, every row in a single GridLayout is the same height, and every cell is the same width.
Provide ASCII art (or an image with a simple drawing) of the GUI as it should appear in smallest size and (if resizable) with extra width/height.

Related

Best way to create an interactable grid

What I want is a little unusual. I want to make a screen that shows me a number squares inside of it. I should be able to determine the number of rows and lines by two integers. I also want to be able to set for example colors of the squares, and they should be clickable so they need to have an id that is numbered and an onClick function.
For example:
height=2
rows=3
color1=FFFFFF
color5=000000
is Something like this achievable? How should I approach this?
I think what you're looking for is a JButton.
For your particular problem, consider creating a 2D Array of JButtons and display them using a JPanel and JFrame. The size height and width of your grid would be the length of each of these arrays. E.g.
myArray.length() is the width (number of columns in the grid) and myArray[0].length() is the height of the grid (number of rows).
JButton supports setting images and colours.
Documentation:
https://docs.oracle.com/javase/tutorial/uiswing/components/button.html

How do I get the width & height of each grid cell in GridBagLayout?

Everywhere I look for help with GridBagLayout only helps with GridBagConstraints. I understand that part completely, it's the methods inside GridBagLayout that confuse me.
So I realized that the fields columnWidths and rowHeights are for overriding GridBagLayout's cell widths and heights, and are null until set by user. (I have implemented those fields in my program below) So how do I get the ACTUAL width and height of the cell?
I'm basically trying to override the paint method of the container in order to draw a grid that show where each cell begins and ends (where gridwidth and gridheight is irrelevant). The one thing that looks applicable is getLayoutInfo(Container parent, int sizeflag), but GridBagLayoutInfo has no methods or fields, and I have no idea what it means by sizeflag.
EDIT:
This is basically the grid I would like to draw, but of course I want to make sure it would work on any container where gridlayout is the layout manager. These are the actual gridx and gridy coordinates highlighted in red. I just don't know how to get the values I need to the paint method.
GridBagLayout has a method getLayoutDimensions() which returns array of two arrays. First array is column widths and second array is row heights. This method considers insets set in GridBagConstrains of each cell to be part of the cell.
I know this question is stale but I ran into simmilar problem recently and found it while searching for solution.

Java swing GridLayout adjusting cellsize

After starting a JPanel using GridLayout(4,4) i insert a JLabel (and attach an imageicon to it) inside every grid cell with size of (150,150).
when i resize the JLabel to size (100,100) the image get cropped (which is perfectly fine by me), but i get a wierd looking grid (imaged added at the end).
if this helps: i dont actually resize the window, i just need to make sure the the size of the JLabel is set to (100,100) always, no metter what is the original image size.
before:
http://postimg.org/image/iolyeb8e7/
after:
http://postimg.org/image/5j6g87ein/
thanks
Unfortunately you did not say what you expect the grid to look like. I assume you don't want the cells to be so far apart from each other.
The GridLayout documentation states that...
The container is divided into equal-sized rectangles, and one component is placed in each rectangle.
If you shrink the size of each JLabel (i.e. the components in each of those rectangles) you just do that. You shrink the size of the component, not that of the rectangle. The grid does not care if the component is to small to fill the whole rectangle. At the moment you add the component to the grid1, the grid tries to set the components size to best fit the available space. But if you later change the labels size, the grid does not care.
What you probably want is to change the size of the whole grid: If you set the grids size to 400 by 400 it should evenly divide it to all 4 rows and 4 columns, so you get rectangles of size 100 by 100. All labels should automatically be sized accordingly.
1 Probably it is not exactly while adding the labels but while validating the container, but I don't know all the internal details about how and when layouts do there magic.

How to make a component occupy all the available space in GridBagLayout?

In my Swing application I have a panel that uses a GridBagLayout. There, I have 3 columns and in one of these columns there is a JTextField. What I want is to set the width of this JTextField (using setPreferredSize() method) so that it occupies all the available space of its column.
As I understand the widths of the columns are calculated based on the weights that I've assigned to them using GridBagConstraints. I tried to subtract from the width of the panel the sum of widths of other components at the same line, but that doesn't seem to work, apparently because of different margins and insets.
So, how do I make my JTextField occupy all the available space?
Take a look at ...
GridBagConstraints.weightx
GridBagConstraints.fill
These will allow you to control how a component fills a given cell and the amount of "weight" a cell is given over the others

Calculating dimensions of a cell in a JTable

How can I calculate the dimenstions of the cell in JTable containing various components to calculate the height of table dynamically.
For example, I have a table containing multiple checkboxes in the 3rd column and various components in other columns. When new components are added to the cell, then the height and width of the whole table should change dynamically.
Thanks in advance.
You might be able to leverage the approach shown here, which invokes setRowHeight() to accommodate the maximal height of each column's renderer.

Categories