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
Related
I am creating a minesweeper game in Java, and I want my JFrame to be in the exact size to be able to see all of the buttons and without margin.
I have tried to calculate the size per number of tiles, and it didn't really work. Is there any way I can make the JFrame to "auto size" by the size of the tiles?
You can use .setBounds(x,y,width,height) method for the JFrame. For the width and height, create an int counter which stores the number of tiles you have on screen. I am guessing you are using buttons are the tiles? So if the user chooser the difficulty, they are basically choosing the number of tiles that the board will be. e.g. easy mode: 20x20 tiles, medium mode: 50x50 tiles, etc. So if they choose easy, set the counter number (in the actionListener of the "Easy Mode" button to '20'. Then do yourJFrame.setBounds(0,0,counter,counter); This should work for a square board.
If you want a different shape, change the above counter to "counterX" for x-axis. Then make another int counterY'. This is for the y-axis. In this case, your code will beyourJFrame.setBounds(0,0,counterX,counterY);`
This should work. Have fun!
May be try this.
((JFrame)myButton.getTopLevelAncestor()).pack();
Where myButton is the button whose text is modified during execution.
For my Java Swing app I want to display a grid of items, fixed to two columns, and for the elements (grid items), I want to use fixed height, and if due to its height they don't fit, place a vertical scrollbar.
For anyone who knows a bit about Android development, is more or less what GridView does perfectly.
So, I'm trying to find a way to do that. I tried with a JPanel with GridLayout inside a JScrollPane, but in this case the elements get too much width and I also need a horizontal scrollbar, which definitely is not what I want.
So, what do you suggest?
Thank you.
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.
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'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.