Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine overlapping components in Java Swing

Tags:

java

swing

I have a JPanel loaded on top of JXTreeTable. I want to determine if JPanel overlaps a particular cell of JXTreeTable. I tried retriving the X and Y components and comparing them. However when I check the JPanel's minimum X and Y co-ordinates, they are always 0.0. I am not able to figure out that. Is there any other than than just comparing the co-ordinate values?

like image 408
DarkKnight Avatar asked Aug 13 '26 09:08

DarkKnight


1 Answers

You should be able to use Rectangle to help determine if the components are overlapping.

ie

Rectangle panelBounds = panel.getBounds();
Rectangle cellBounds = getRowBounds(row); // getPathBounds(path);
if (panelBounds.intersects(cellBounds)) {
    // We have a collision
}

Now, you bigger problem is, the X/Y return value always been 0. This means that the Rectangle return by the getBounds method will also return x/y of 0.

You will need share some code to show us how you laying out this component.

like image 137
MadProgrammer Avatar answered Aug 14 '26 23:08

MadProgrammer