Since it seems like it is not possible to have a node selected unless it is visible, I came up with this solution. After debugging into the events, I found that the tree is always "invalid" when I collapse a node. So I override fireValueChange() in JTree. If the tree is "valid" I let the value change happen. Otherwise I clear the selection. If I don't clear the selection, the collapsed node is selected in UI, which gives the wrong impression, since it isn't logically selected. By clearing the selection it is then possible to explicitly select the collapsed node. This solution is closest to what I want. Only thing missing is the invisible node being selected if I expand the previously collapsed node again.
So far it works well for me:
this.tree = new JTree(this.rootNode)
{
@Override
protected void fireValueChanged(final TreeSelectionEvent e)
{
if (this.isValid())
super.fireValueChanged(e);
else
{
super.clearSelection();
}
}
};