You need to get the closest point on the line (segment) to the point clicked, then you can compare the distance between your click point and this point to see if it is within a certain threshold.

A 3D version in C++ for closest point on segment to point:

Vec3 closestPointOnSegmentToPoint(const Vec3 &segment1, const Vec3 &segment2, const Vec3 &point)
{
Vec3 ab = segment2 - segment1;
if(dotVectors(ab, ab) < 0.01f) return ab;
float t = dotVectors(point - segment1, ab) / dotVectors(ab, ab);
if(t < 0.0f) t = 0.0f;
if(t > 1.0f) t = 1.0f;
return segment1 + (t * ab);
}

That will return the point on the segment that is closest to the point input. You can then just form a vector from this point to the click point and check its length to see if it is close enough to be considered clicked on the line.

Appreciate a lot of that is over your head if you are new to this. Suggest reading up on 2D vector stuff, looking at what support Java has for vector math and so on.