I have been looking for a reason for this to no avail. I have a JList with an EmptyBorder (for some margin). It is in a JScrollPane. When there is a list entry that is wider than the viewport, I can click on the JList and if I move the mouse cursor any when clicking, the JList scrolls to the left, the width of the border, putting the text up next to the edge of the viewport again. This is rather annoying as it defeats the purpose of using the EmptyBorder for margin.
Right. I see where that behavior is coming from. Take a look at line 2761 of BasicListUI.java, in the handler's mouseDragged(...) method.
One way to get the behavior you desire is to use a custom renderer and set the left/right borders to the renderer, and the top/bottom borders to the JList.You'll need to test whether that works with all LaFs.
There are no new questions, but there may be new answers.
It works on all the available L&F in NetBeans. I just added it to the form through the custom code properties, pre-adding for the JLabel (Lines 28-45), although I could add it in my controller. I see how it adds the border to the cell and no matter if I add it before or after adding a border to the JList, the code takes that into account. But in my case I don't need the border on the JList. Thanks again.
The check for a null border is not needed in my case. What it does is still detects a border, it is never null, even though I do not add one it still creates a compound border. This over pads the cell plus it causes a very thin dotted line to show around a cell when it is selected. So all I need is this code: