I'm working in a project of Air-lain class
I want to create a array of buttons to reserve seat.
I do the array but I don't know how to put it in the left of the frame.
it is be in the whole of frame
int x=0;
for (int j=0 ; j<100 ;j++)
{
a[x][j]=new JButton();
a[x][j].setBackground(Color.GREEN);
contentPane.add(a[x][j]);
x++;
if(x==5)
x=0;
}
also how can I put a names for the column and row out side the buttons
What you need is a GridLayout/GridBagLayout. Refer here and here
GridLayout is a very simple layout manager that can arrange components
in a grid of rows and columns. Every cell in the grid, and thus every
component, is resized to be the same exact size.
GridBagLayout is much more complex, and much more flexible. It also arranges components in rows and columns, but individual components can span multiple rows or multiple columns and thus the components in the grid can vary in size.
I think that you should illustrate your problem better. How about a simple paint image with drawing with what you want? I am sure there is insufficient information here to answer your problem.
Related
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
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.
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.
I have a List<JToggleButton> and I need to add all those buttons to a panel.
The panel needs to have only a vertical scrollbar. It has a fixed size of 600 x 600 px.
Buttons have different sizes and I need them look pretty compact (for example, some rows can have two big buttons, some four smaller). I need to add the buttons by order from list (first with index 0, then 1 and so on..).
How to achieve this layout?
You can try placing the JPanel with a FlowLayout in a JScrollPane and add all the buttons.
I have two JPanels (let's call these Panel1 and Panel2). These panels are of the same width, but varying heights.
I want to put these JPanels into one big JPanel (lets call it Panel0), and stack them on top of each other (I decided to set Panel0's layout as GridLayout(0,1)).
The problem, is that both nested panels (panels 1 and 2) end up having the same dimensions (those of the biggest between the two), instead of the setPreferredDimension and setDimension that I set to them.
Sorry, I can't really provide any code (there's a lot of crap added to the panel's, and it's all for something work-related). Any advice? Thanks!
GridLayout forces all components to be the same size; that's why it's called a grid.
Since you only have two panels, I'd suggest using a BorderLayout with one panel at NORTH and the other CENTER. If you allow resizing, then the one in CENTER will be the expand to fill any extra vertical space, so just be aware of that.