Around the time of the Sengoku period they had pretty normal dealing with foreigners in terms of trading and learning. Then, when foreign influences started disturbing their country (like the introduction of firearms, Christianity, and practices like drugs) they began their mistrust towards foreigners. Firearms particularly caused the alienation of an entire noble class. Christianity also threatened the established spiritual practices in Japan (plus some Japanese Christians were crazy, like Amakusa) These problems slowly piled until around the start of Tokugawa.
So when the Sengoku period ended, and the Tokugawa shogunate began, these problems caused the country to go into isolation in order to preserve its culture. So, those two centuries of people shunning foreign interaction caused the new generations to be generally mistrustful of foreigners. It didn't help that the first foreign interaction was quite rude arrival of Commodore Perry's black ships (which not only were loud and generally dirty, but also deadly). Since then that mistrust has been ingrained into succeeding generations.
Still, that did not prevent them from using foreigners to their advantage. The Japanese navy's quick rise to power in the turn of the 20th century was due to the Japanese sending their naval officers to study in Britain. When the Jews started leaving Europe the Japanese gave them a place to stay in order to learn the technical skills the European Jews were known for. So, to some extent there is this respect for foreigners. It's just that they'd rather not let the relationship go any further than that.