aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Find out if there is a component in a specific cell of GridBagLayout? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Find out if there is a component in a specific cell of GridBagLayout?" Watch "Find out if there is a component in a specific cell of GridBagLayout?" New topic
Author

Find out if there is a component in a specific cell of GridBagLayout?

Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
Hi everyone, it's been awhile, but I'm glad to be back.
I'm making a program to layout photos in a collage, grid style. I've created a custom component that extends JPanel, called PhotoPanel. Each PhotoPanel paints it's photo on the JPanel with a transparent background. I layout the PhotoPanels in a JPanel (called the PagePanel) using GridBagLayout. Each PhotoPanel has an instance of GridBagConstraints. When properties are changed for either the page or the selected PhotoPanel, the selected PhotoPanel's GridBagConstraints is updated, and the page layout is redone: e.g. all components are removed from the PagePanel and then readded using their own, updated GridBagConstraints object. The idea is that each photo can be set up to occupy multiple cells in the grid, using the GridBagConstraints 'gridwidth' and 'gridheight' properties. My method to add all of the PhotoPanels to the PagePanel keeps track of 'row', 'column', 'maxRows', and 'maxColumns' variables to control where the PhotoPanels are added. They are simply added in sequential order like so:

set the PhotoPanel's constraints, including 'row' and 'column'
page.add(PhotoPanel, PhotoPanel.myConstraints);
column++
if(column >= maxColumns){
column = 0;
row++;
}

What I want is to check the GridBagLayout to see if there is already a component occupying the cell at position "row,column" so that I can skip that cell. My current approach is that I create a boolean array of size [maxRows][maxColumns] and then set the "cells" where I add components to true. For components that occupy multiple cells on the PagePanel, multiple "cells" in the array are set to true in the proper positions. It works the way I want it to right now, but I feel like it would be more reliable to be able to "ask" the GridBagLayout if a certain cell is already occupied. Is there a way to do this? or should I just stick with my boolean array?


Life is 10% what happens and 90% how you react to it.
Attitude is everything.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if you know the location and dimension of each grid cell, perhaps getComponentAt() might work for you.
e.g. say your grid is 10 x 10, each 50 x 50, and the cell at 2,2 (3rd column, 3rd row) has it's rectangle at (100,100,50,50)

if you did this
System.out.println(pagePanel.getComponentAt(125,125).getClass().getName());//where 125,125 is middle of cell
it should print out either PagePanel, or PhotoPanel
if PagePanel, that cell is not occupied
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Find out if there is a component in a specific cell of GridBagLayout?