TabLabel (Axis measurer, TabLabel... labels) { double freeSpace; if (measurer.getMinValue(dragRec) < measurer.getMinValue(myDragOriginalRec)) { freeSpace = measurer.getMaxValue(myDragOriginalRec) - measurer.getMaxValue(dragRec); } else { freeSpace = measurer.getMinValue(dragRec) - measurer.getMinValue(myDragOriginalRec); } int max = -1; TabLabel maxLabel = null; for (TabLabel each : labels) { if (each == null) continue; final Rectangle eachBounds = each.getBounds(); if (measurer.getSize(eachBounds) > freeSpace + freeSpace *0.3) continue; Rectangle intersection = dragRec.intersection(eachBounds); int size = intersection.width * intersection.height; if (size > max) { max = size; maxLabel = each; } } return maxLabel; }