DarkKnight
DarkKnight

Reputation: 243

Determine overlapping components in 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?

Upvotes: 3

Views: 943

Answers (2)

mKorbel
mKorbel

Reputation: 109815

you can to determine the JComponents coordinates only if:

  • is already visible on the screen

  • after called method pack()

for better help sooner post an SSCCE

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347194

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.

Upvotes: 3

Related Questions