Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - JList Clicking

Tags:

java

swing

jlist

I have a JList with a JScrollPane. If I only have one thing in the JList, there will be empty space due to the JScrollPane. If I click anywhere in the JList, it will click the one element. Is there a way to make it only click the element if I actually click it? Also, I am using a custom renderer for the JList to add images, if that has anything to do with it.

Thanks

like image 829
Stripies Avatar asked Jun 08 '26 05:06

Stripies


1 Answers

You can check the index that was clicked via locationToIndex.

Returns the cell index closest to the given location in the list's coordinate system. To determine if the cell actually contains the specified location, compare the point against the cell's bounds, as provided by getCellBounds. This method returns -1 if the model is empty.

like image 144
mre Avatar answered Jun 10 '26 19:06

mre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!